'How to prevent chrome from using up my memory and swap space on Linux?

My laptop (running Xubuntu 16.04) is a few years old, on an i7-3635QM processor. 3-4 yr ago, it had only 4GB ram, and the chrome browser often completely used up the physical memory and started using swap space (4GB) - when this happens, the entire computer is extremely slow and almost hangs. I had to kill chrome to release all the memory, but the computer is barely responding when it is on swap space.

So, I upgraded it to 16GB memory many years ago, this happened less frequently, but still, for a couple of times in a month, I still occasionally ran into this situation.

At work, I have a Xubuntu desktop running on 32 GB ram, I open as many, if not more, tabs on that desktop, but I rarely see total memory consumption from chrome to exceed 50% of the total memory, and almost never forced the system to use swap.

It feels quite strange that there is such a different behavior for a 16GB vs 32GB. The only thing I can think of is the laptop has an earlier CPU (i7-3635QM) vs the desktop (i7-7700k), but the OS versions and kernel versions are the same.

Does anyone know how to prevent chrome from throwing my system from using swap? I tried to tune the swappiness of the kernel but there was no noticeable change.

thanks



Solution 1:[1]

On Linux you can use Control Groups to limit either CPU or Memory. In your case you can do (adjust the size accordingly):

cgcreate -g memory:chrome
echo 500M > /sys/fs/cgroup/memory/chrome/memory.limit_in_bytes

And then run chrome like this:

cgexec -g memory:chrome  /usr/bin/google-chrome-stable %U

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aphroz