Firefox has a long standing bug that leads to rampant memory usage. When you open a large XML file Firefox goes nuts and uses up all the memory in your system. This can cause severe disk thrashing as the OS starts using swap space. To prevent this it makes sense to limit Firefox’s memory usage.
To limit a process’ memory usage in Linux you use the ulimit command
ulimit -Sv <amount of memory in kilobytes>
For example: Limit virtual memory usage to 500MB:
ulimit -Sv 512000
The number is virtual memory usage which I think includes both the resident set size and shared memory. It is a soft limit and only applies to processes that run in the same shell instance. So to limit Firefox’s memory usage you create a short cut that calls ulimit and then Firefox:
ulimit -Sv 512000 && firefox
What happens if Firefox tries to allocate more memory than the limit? The system will kill the Firefox process. So set a high enough limit that it will never hit it during normal usage. It’ll only be a safety net that comes into play in extraordinary situations.
One thought on “Limit Firefox memory usage on Linux”
I’ve “only” got 24G of memory in total (including 16G swap) on the machine running firefox, which might seem a lot, but recently I noticed the system becoming very slow and discovered that firefox appeared to be using (according to htop) ~ 56G of virtual memory! So this could be a useful way mitigate against such runaway processes in the future. Thanks.