Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Limit Chrome from eating all the memory and CPU

I was tired of Chrome eating all my laptop resources so I decided to put some limit to it with cgroup.

As I was using Ubuntu 12.04 with support for cgroup, I installed the package cgroup-bin and add the following group to the file /etc/cgconfig.conf:

group browsers {
    cpu {
#       Set the relative share of CPU resources equal to 25%
        cpu.shares = "256";
    }
    memory {
#       Allocate at most 1 GB of memory to tasks
        memory.limit_in_bytes = "1G";
#       Apply a soft limit of 512 MB to tasks
        memory.soft_limit_in_bytes = "768M";
    }
}

Then I added one new rule to the file /etc/cgrules.conf to add any new Chrome process launched by my own user (jojeda) to the cgroup browsers:

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

And then I restarted the cgconfig service:

$ sudo service cgconfig restart

Now all the new chromium process that I'll launch with my user jojeda will be under the cgroup browsers with its memory and cpu limits.

@juanje

This comment has been minimized.

Copy link
Owner Author

@juanje juanje commented Mar 29, 2014

Of course those limits are OK for my system and my needs, but they will depend on your memory and your usage of the browsers.

@pyKun

This comment has been minimized.

Copy link

@pyKun pyKun commented Jul 15, 2014

Thank you, juanje. I'm new to cgroups and many mistakes in cgroups maunals mislead me. Your example really help me:)

@magwas

This comment has been minimized.

Copy link

@magwas magwas commented Jan 13, 2017

I use xenial. Cannot find any of the files mentioned, and no cgmanager service. However apparently cgroup is well and alive, as I see a lot of mounts of type cgroup. What I am missing?

@gunar

This comment has been minimized.

Copy link

@gunar gunar commented Feb 22, 2017

Thanks!
It wouldn't work for me without this part:

   perm {
      task {
        uid = gunar;
        gid = users;
      }
      admin {
        uid = gunar;
        gid = users;
      }
    }

See gunar/dotfiles@846c258

@maxkoryukov

This comment has been minimized.

Copy link

@maxkoryukov maxkoryukov commented May 3, 2017

@juanje, probably, there is a typo:

#       Apply a soft limit of 512 MB to tasks
        memory.soft_limit_in_bytes = "768M";

768 ≠ 512

@gunar

This comment has been minimized.

Copy link

@gunar gunar commented Sep 14, 2017

Okay so cpu.shares allows you to distribute CPU across multiple cgroups.
It does nothing if you have a single cgroup -- just one for chromium.
CPU throttling is necessary for me, and for you too probably.
Otherwise, I get system-wide freezes -- whenever chromium is trying to free up memory? Maybe.

Here's my current setup. Initial results are promising.

group browsers {
  perm {
    task {
      uid = gunar;
      gid = users;
    }
    admin {
      uid = gunar;
      gid = users;
    }
  }
  cpu {
    # Allow chromium to use 6 CPU cores maximum (current machine has 8 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 =  600000;
    cpu.cfs_period_us = 100000;
  }
  memory {
    # Allow chromium to use 6GB maximum (current machine has 8GB RAM)
    memory.limit_in_bytes = "6G";
    memory.soft_limit_in_bytes = "5G";
  }
}

See gunar/dotfiles@6acc2a8

@hkmaly

This comment has been minimized.

Copy link

@hkmaly hkmaly commented Apr 10, 2018

In Gentoo, you need libcgroup package, the configuration files are in /etc/cgroup/ directory (I actually needed to change the path in /etc/cgroup/cgred.conf to match) and in addition to cgconfig service you need to run (restart) cgred service so the cgrules.conf is interpreted.

@kalimalrazif

This comment has been minimized.

Copy link

@kalimalrazif kalimalrazif commented Jun 5, 2019

One question? This limits are apply for all the chrome processes? 1G for process or it is the maximum memory and cpu for alll the chrome process?

Thanks in advance
Nomar

@juanje

This comment has been minimized.

Copy link
Owner Author

@juanje juanje commented Jun 6, 2019

@kalimalrazif This is very old config and I don't even use it anymore, but I think is 1G for all the processes.
The config defined 1G for the group, so the 1G is to be shared by all processes fo the group. I think...

@kalimalrazif

This comment has been minimized.

Copy link

@kalimalrazif kalimalrazif commented Jun 6, 2019

Ok all the processes then :-) thanks anyway for your answer :-)

@tobia

This comment has been minimized.

Copy link

@tobia tobia commented Mar 6, 2020

For those who want an up-to-date tutorial for Ubuntu, here is one https://www.paranoids.at/cgroup-ubuntu-18-04-howto/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.