Limiting use of RAM in Chrome?

Solution 1:

  1. I wrote a Python 2.5 program which kills chrome's renderers when they use over a set amount of memory. I run this program under watch. (note that it uses the psutil module which isn't included with Python.)

    import sys, os, psutil
    
    if len(sys.argv) == 2:
        try:
            limit = int(sys.argv[1])
        except:
            limit = 200 # default 200MB
    else:
        limit = 200
    
    uid = os.getuid()
    for p in psutil.get_process_list():
        try:
            if (p.name == 'chrome' and any('type=renderer' in part for part in p.cmdline)
               and p.uid == uid):
                m = p.get_memory_info()
                #print p.pid,m, m.rss / 1024 / 1024, m.vms / 1024 / 1024
                if (m.rss / 1024 / 1024) > limit: # kill if rss is greater than limit
                    print 'Killed', p.pid
                    p.kill()
        except psutil.error.NoSuchProcess:
            pass
        except psutil.error.AccessDenied:
            pass
    
  2. I rely on Session Buddy to recover the open tabs when chrome fails to restore them.

Solution 2:

The only thing I've seen to date that can do this is to run chrome inside a container and limit the containers ram.

However this has some major caveats,

  • Running chrome is complicated by the dockerize setup and launch sequence

  • for one, Chrome already uses kernel containers to sandbox its threads; so you have to run the container with a kind of root privilege that allows that to work. This can be circumvented, and the linked container model does so. (it does practically everything it needs to)

  • You will almost certainly loose gpu acceleration

  • getting audio to work is complicated, but handled in the linked container model.

  • Whatever else you expect to go wrong when you void your warranty, Chrome violently dislikes being told not to use more ram, and will act up and tantrum accordingly.

But it ultimately does work.

I am more interested in applying these ram limits to Electron Shell apps which don't have prebuilt docker images to rangle them for you.


Off topic but I want to note that Firefox is very well behaved on limited hardware, but I don't consider that a real answer.