Friday, March 18, 2011

Limiting Flash CPU usage in Linux (Firefox only)

Firefox makes it easy to enable/disable plugins like Flash.

The problem is that there are many webs out there. Some of them use Flash legitimately and with good intentions, while some others try to invade us with ads banners without taking a minimal effort to keep resource usage low. Switching on/off the flash plugin is not always an option or at least not a confortable one. Ideally it would be great to have Flash running while simultaneously get certain that it will not peak CPU usage slowing down web navigation.

Newer versions of Firefox use an external process named "plugin-container" to execute external plugins like Flash. Fortunately that makes it quite easy to control the maximum CPU percentage we want to assign to Flash. All we need is to follow the next 2 steps:

- Install the command line utility "cpulimit". How to do it must be straightforward if its already in our favority linux distro repository.

- Edit (as root) /etc/rc.local and add a line similar to:

cpulimit -l 20 -e plugin-container &

Once we reboot our system 'cpulimit' will take care that external firefox plugins (aka flash) don't get more than 20% of our CPU. That can be too slow for people who want to check videos on youtube, so they will probably want to use a higher percentage. 20% is just a personal option. (Don't forget also that VLC allows to play youtube videos and it performs much better than the Flash plugin:

In my particular system, a "not so 3-years-old" laptop, I'm used to set cpu frequency to the minimum available clock, 800Mhz. That let's me use the laptop as a real LAPtop, I mean without my laps being burn by the heat dissipated by the CPU. Before using cpulimit I had no other option that turning off the plugin flash since randomly some "aggresive" flash banner knocked down my web browser. After cpulimiting the plugin those banners will need to manage themselves how to share the 20% cpu left for them. Not my problem anymore.

What about Chrome? I think, for what I have read about, that Google Chrome is ussing some type of plugin container too, but I found no easy way to control it. "top" just show a single process for the browser and the plugin so I didn't find any easy way to let cpulimit control it. Any hint/idea is wellcome!

No comments: