Skip to content

Instantly share code, notes, and snippets.

@ar45
Created January 11, 2018 19:18
Show Gist options
  • Save ar45/3c977311c9b8f32948eeca25a7ba2198 to your computer and use it in GitHub Desktop.
Save ar45/3c977311c9b8f32948eeca25a7ba2198 to your computer and use it in GitHub Desktop.
Limit Google Chrome Memory Hungry Eating up 32GB of ram
[Unit]
Description=Chrome browsers cgroup
After=multi-user.target
[Service]
ExecStart=/bin/bash -x -c '[ -d /sys/fs/cgroup/memory/user.slice/chrome ] || /bin/mkdir /sys/fs/cgroup/memory/user.slice/chrome; chmod 766 /sys/fs/cgroup/memory/user.slice/chrome/tasks; echo "Satrting"; CHROME_MEMORY_LIMIT_IN_BYTES=${CHROME_MEMORY_LIMIT_IN_BYTES:-5368709120}; echo "${CHROME_MEMORY_LIMIT_IN_BYTES}" >/sys/fs/cgroup/memory/user.slice/chrome/memory.limit_in_bytes; echo "Setting CHROME_MEMORY_LIMIT_IN_BYTES=${CHROME_MEMORY_LIMIT_IN_BYTES}";'
;ExecStart=/bin/mkdir /sys/fs/cgroup/memory/user.slice/chrome
;ExecStop=/bin/rm /sys/fs/cgroup/memory/user.slice/chrome
EnvironmentFile=-/etc/environment
Type=oneshot
[Install]
WantedBy=multi-user.target
@ar45
Copy link
Author

ar45 commented Jan 11, 2018

Install systemd unit to initialize the cgroup

sudo wget https://gist.githubusercontent.com/ar45/3c977311c9b8f32948eeca25a7ba2198/raw/chrome-cgroup.service \
  -O /etc/systemd/system/chrome-cgroup.service
sudo systemctl daemon-reload
sudo systemctl enable chrome-cgroup.service
sudo systemctl start chrome-cgroup.service

Add the following line to /usr/bin/google-chrome or wherever your chrome is installed at the beginning of the shell script.

echo $$ >/sys/fs/cgroup/memory/user.slice/chrome/tasks

To move existing chrome processes to the new cgroup, do

for pid in `ps --no-headers -fC chrome | awk '{print $2}'`;
do
    echo $pid | tee /sys/fs/cgroup/memory/user.slice/chrome/tasks;
done;

Set environment variable

echo CHROME_MEMORY_LIMIT_IN_BYTES=5G | sudo tee -a /etc/environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment