Skip to content

Instantly share code, notes, and snippets.

@jakewarren
Last active January 11, 2024 02:33
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jakewarren/477ecd1149abe908cbd5cf7a7c9abaa3 to your computer and use it in GitHub Desktop.
Save jakewarren/477ecd1149abe908cbd5cf7a7c9abaa3 to your computer and use it in GitHub Desktop.
Restrict the amount of CPU and memory resources that Chrome can consume.

Restrict the amount of CPU and memory resources that Chrome can consume.

Tested on Ubuntu 16.04/Linux Mint 18.


Install cgroups:

sudo apt install cgroup-bin

Set up a browser group in /etc/cgconfig.conf to limit the amount of resources Chrome can use:

group browsers {
  perm {
    task {
      uid = jake;
      gid = users;
    }
    admin {
      uid = jake;
      gid = users;
    }
  }
  cpu {
    # Allow chromium to use 2 CPU cores maximum (current machine has 4 cores).
    # See: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sect-cpu-Example_Usage.html
    cpu.cfs_quota_us =  200000;
    cpu.cfs_period_us = 100000;
  }
  memory {
    # Allow chromium to use 6GB maximum (current machine has 16GB RAM)
    memory.limit_in_bytes = "6G";
    memory.soft_limit_in_bytes = "5G";
  }
}

Then add a rule to /etc/cgrules.conf so that any new Chrome processes launched by my own user (jake) are added to the cgroup browsers:

# user:process                                        subsystems      group
jake:/usr/lib/chromium-browser/chromium-browser       cpu,memory      browsers

Since Ubuntu doesn't provide a systemd service to start cgroups, create your own in /etc/systemd/system/cgroups.service:

[Unit]
Description=Load cgroup configs
After=remote-fs.target

[Service]
Type=forking
ExecStartPre=/bin/echo "Processing /etc/cgconfig.conf..."
ExecStartPre=/usr/sbin/cgconfigparser -l /etc/cgconfig.conf
ExecStartPre=/bin/echo "Processing /etc/cgrules.conf..."
ExecStart=/usr/sbin/cgrulesengd --logfile=/var/log/cgrulesengd.log
Restart=on-failure

[Install]
WantedBy=multi-user.target

Finally, enable & start the service to load up your rules

sudo systemctl enable --now cgroups.service

References:

https://gist.github.com/juanje/9861623
https://wiki.archlinux.org/index.php/cgroups#Persistent_group_configuration
https://askubuntu.com/questions/836469/install-cgconfig-in-ubuntu-16-04
https://bugs.launchpad.net/fuel/+bug/1674633

@bartman
Copy link

bartman commented Apr 12, 2019

Thanks. This worked great on Debian/testing.

Here is a script that will "watch" the memory usage counters for the browser group.
https://gist.github.com/bartman/876aa722c071cdcf1115fd468625840c

@mikkorantalainen
Copy link

Note that you shouldn't be running system with systemd combined with other software that messes with cgroups. For details, see https://askubuntu.com/a/1108189/50254

@bartman
Copy link

bartman commented Feb 20, 2023

Note that you shouldn't be running system with systemd combined with other software that messes with cgroups. For details, see https://askubuntu.com/a/1108189/50254

In that post you said:

Unfortunately I cannot find any info about how to do that so probably the only way is to start reading systemd documentation and restart from the scratch.

Do you have any advice 3 years later?

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