<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: msugakov</title>
    <description>The latest articles on DEV Community by msugakov (@msugakov).</description>
    <link>https://dev.to/msugakov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F565490%2F9a3c047e-2aeb-426b-b3c5-58bdb8c38644.jpeg</url>
      <title>DEV Community: msugakov</title>
      <link>https://dev.to/msugakov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/msugakov"/>
    <language>en</language>
    <item>
      <title>Taking Firefox memory usage under control on Linux</title>
      <dc:creator>msugakov</dc:creator>
      <pubDate>Sat, 23 Jan 2021 00:19:01 +0000</pubDate>
      <link>https://dev.to/msugakov/taking-firefox-memory-usage-under-control-on-linux-4b02</link>
      <guid>https://dev.to/msugakov/taking-firefox-memory-usage-under-control-on-linux-4b02</guid>
      <description>&lt;p&gt;Life was easy when Firefox was 32-bit and single-process.&lt;br&gt;
In 2021 it is 64 bit, launches bunch of sub-processes, and with many open tabs I routinely see Firefox processes allocate 16Gb of ram and 20Gb of swap slowing down everything else.&lt;/p&gt;

&lt;p&gt;I'm using Ubuntu 20.04 and here's what helped me contain unbounded memory usage. Steps for other Linux distros might be similar.&lt;/p&gt;

&lt;h3&gt;
  
  
  0. Allow tab unloading happen
&lt;/h3&gt;

&lt;p&gt;If you're running Firefox 93 or newer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open &lt;code&gt;about:config&lt;/code&gt; page.&lt;/li&gt;
&lt;li&gt;Find &lt;code&gt;browser.low_commit_space_threshold_mb&lt;/code&gt; value there.&lt;/li&gt;
&lt;li&gt;Set it to some high value, e.g. 2/3 of total memory available in your system.

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;free --mebi&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Find &lt;code&gt;Mem&lt;/code&gt; / &lt;code&gt;total&lt;/code&gt; value and take approximately 2/3 of it.
For example, my machine has &lt;code&gt;32123&lt;/code&gt; MiB total memory, I set &lt;code&gt;browser.low_commit_space_threshold_mb&lt;/code&gt; to be &lt;code&gt;24000&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  What this does?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Short version
&lt;/h4&gt;

&lt;p&gt;This makes tab unloading mechanism always active as opposed to the default setup when tab unloading kicks in only when less than 200 MiB or less than 5% of total memory remains available.&lt;/p&gt;

&lt;h4&gt;
  
  
  Longer read
&lt;/h4&gt;

&lt;p&gt;Since version ~93, Firefox supports &lt;a href="https://support.mozilla.org/en-US/kb/unload-inactive-tabs-save-system-memory-firefox" rel="noopener noreferrer"&gt;unloading tabs&lt;/a&gt; that were not visited recently.&lt;/p&gt;

&lt;p&gt;The browser periodically reads &lt;code&gt;/proc/meminfo&lt;/code&gt; and checks &lt;code&gt;MemTotal&lt;/code&gt; and &lt;code&gt;MemAvailable&lt;/code&gt; entries there. Once &lt;code&gt;MemAvailable&lt;/code&gt; drops less than &lt;code&gt;browser.low_commit_space_threshold_percent&lt;/code&gt; (5% by default) of &lt;code&gt;MemTotal&lt;/code&gt; or &lt;code&gt;MemAvailable&lt;/code&gt; falls below &lt;code&gt;browser.low_commit_space_threshold_mb&lt;/code&gt; (200 MiB by default), the tab unloading mechanism is notified to do its job. Links: &lt;a href="https://hg.mozilla.org/integration/autoland/rev/44b7971a0893" rel="noopener noreferrer"&gt;implementation&lt;/a&gt;, &lt;a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1532955" rel="noopener noreferrer"&gt;ticket&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For me the problem is that Firefox is by far not the only process on the system which needs a lot of memory. The defaults are chosen quite conservatively and don't account for Firefox own contribution to the memory pressure.&lt;br&gt;&lt;br&gt;
Moreover, &lt;code&gt;/proc/meminfo&lt;/code&gt; does not respect &lt;code&gt;cgroups&lt;/code&gt; (more info in &lt;a href="https://fabiokung.com/2014/03/13/memory-inside-linux-containers/" rel="noopener noreferrer"&gt;this article&lt;/a&gt;) which I use below to put limits on swap and memory usage, therefore the browser is unable to understand when its memory usage is approaching the limit I configured for it with cgroup.&lt;/p&gt;

&lt;p&gt;Setting &lt;code&gt;browser.low_commit_space_threshold_mb&lt;/code&gt; to aggressively high value should make tab unloading always on. In my experience, this greatly reduces memory footprint except when I open lots of tabs quickly. This also functions like a garbage collector and prevents memory footprint from constantly growing until the browser gets killed.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;You may stop here and see if forcing tab unloading helps, or you may proceed with further suggestions to prevent Firefox from using swap and put a hard limit on the amount of memory it can use.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Enable cgroups v2 and swap control
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open &lt;code&gt;/etc/default/grub&lt;/code&gt; for editing (don't forget &lt;code&gt;sudo&lt;/code&gt;) and find &lt;code&gt;GRUB_CMDLINE_LINUX_DEFAULT&lt;/code&gt; there. Add &lt;code&gt;systemd.unified_cgroup_hierarchy=1&lt;/code&gt; and &lt;code&gt;swapaccount=1&lt;/code&gt; to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how my looks like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;GRUB_CMDLINE_LINUX_DEFAULT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"quiet splash systemd.unified_cgroup_hierarchy=1 swapaccount=1"&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;sudo update-grub&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reboot.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test if cgroups v2 are enabled by running &lt;code&gt;mount -t cgroup2&lt;/code&gt;. The output should be similar to this:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;mount &lt;span class="nt"&gt;-t&lt;/span&gt; cgroup2
cgroup2 on /sys/fs/cgroup &lt;span class="nb"&gt;type &lt;/span&gt;cgroup2 &lt;span class="o"&gt;(&lt;/span&gt;rw,nosuid,nodev,noexec,relatime,nsdelegate&lt;span class="o"&gt;)&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  What this does?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;systemd.unified_cgroup_hierarchy=1&lt;/code&gt; enables cgroups v2 support, that's what will enforce memory limits.&lt;br&gt;&lt;br&gt;
Be aware that once you enable v2, software that uses cgroups v1 (I imagine old Docker versions) may stop working. If you'll run into issues with older software, you know what could be a reason.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;swapaccount=1&lt;/code&gt; enables limiting swap usage. If it isn't enabled, cgroups will only enforce memory limits for RAM but not swap and so Firefox processes will not go over RAM limit but will push everything else into swap.&lt;/p&gt;

&lt;p&gt;Read &lt;a href="https://unix.stackexchange.com/a/531489/49033" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://cateee.net/lkddb/web-lkddb/MEMCG_SWAP.html" rel="noopener noreferrer"&gt;here&lt;/a&gt; if you'd like to learn more about &lt;code&gt;swapaccount&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Edit launcher
&lt;/h2&gt;

&lt;p&gt;I'm using xfce and so I can edit Firefox launch shortcut from the menu. You might do similar depending on how you start the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fj8o6708b4xkiu9t2ubix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fj8o6708b4xkiu9t2ubix.png" alt="Firefox Launcher settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The launch command should be:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

systemd-run &lt;span class="nt"&gt;--unit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;my-firefox &lt;span class="nt"&gt;--user&lt;/span&gt; &lt;span class="nt"&gt;--scope&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nv"&gt;MemoryHigh&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;6G &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nv"&gt;MemoryMax&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;6G &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nv"&gt;MemorySwapMax&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 firefox %u



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;From now use this command/launcher to start the browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What this does?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;systemd-run&lt;/code&gt; allows you to start a process with ad-hoc settings under control of &lt;code&gt;systemd&lt;/code&gt; and cgroups without having to create so-called unit files. In this case we're starting &lt;code&gt;firefox&lt;/code&gt; but you can apply this to other programs of course.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--unit=my-firefox&lt;/code&gt; gives a name to a systemd "scope" where &lt;code&gt;firefox&lt;/code&gt; process will execute. You can omit this parameter or rename it as you wish. I find it useful to give scope some name to be able to more easily diagnose it later.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--user&lt;/code&gt; tells to run scope in your "user" slice, not in "system". This allows process to be started without asking your password.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--scope&lt;/code&gt; configures how process is started. Frankly I don't know what this does, but you need it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-p MemoryHigh=6G&lt;/code&gt; sets 6 gigabyte "soft hard" limit on the amount of RAM Firefox processes may allocate. Firefox may request more memory but OS will resist by slowing down allocation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-p MemoryMax=6G&lt;/code&gt; sets 6 gigabyte "hard" limit on the amount of RAM for Firefox. If it tries to allocate that much, its processes will be killed.&lt;/p&gt;

&lt;p&gt;In my experience, if there is a gap between &lt;code&gt;MemoryHigh&lt;/code&gt; and &lt;code&gt;MemoryMax&lt;/code&gt;, the browser becomes slow and almost unusable once it reaches &lt;code&gt;MemoryHigh&lt;/code&gt;. It made little sense to let it run in this mode especially since it does not react on attempts to close window. Therefore, I prefer to have &lt;code&gt;MemoryHigh == MemoryMax&lt;/code&gt;. This way the browser gets killed after reaching the memory limit and you can start it again without having to kill it from a terminal first.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-p MemorySwapMax=0&lt;/code&gt; does not allow Firefox use swap. In my experiments, Firefox performance gets worse if small amount of swap is allowed. If large amounts (&amp;gt;10G) of swap are allowed, Firefox will actively push out lots of memory there and can run overall ok but it wears SSD.&lt;/p&gt;

&lt;p&gt;Read about Memory* properties in &lt;a href="https://manpages.debian.org/stretch/systemd/systemd.resource-control.5.en.html" rel="noopener noreferrer"&gt;&lt;code&gt;man systemd.resource-control&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Decrease amount of Firefox processes
&lt;/h2&gt;

&lt;p&gt;Note: this setting seems to be gone in recent versions of Firefox :-(&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open browser preferences, &lt;code&gt;about:preferences&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Scroll to "Performance", uncheck "Use recommended performance settings"&lt;/li&gt;
&lt;li&gt;Change "Content process limit" to &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F82hw9038wcti2cmy7umg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F82hw9038wcti2cmy7umg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What this does?
&lt;/h3&gt;

&lt;p&gt;Firefox uses sub-processes to speed up page rendering. The problem is that each sub-process uses lots of RAM. Firefox decides their count automatically and for me the count seems to be 8. Each subprocess using 2Gb of memory easily pushes overall usage to 16Gb or more.&lt;/p&gt;

&lt;p&gt;More info at &lt;a href="https://support.mozilla.org/en-US/kb/performance-settings" rel="noopener noreferrer"&gt;https://support.mozilla.org/en-US/kb/performance-settings&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Test and diagnose
&lt;/h2&gt;

&lt;p&gt;Once you started the browser you can see its memory usage with this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

systemctl &lt;span class="nt"&gt;--user&lt;/span&gt; show my-firefox.scope | &lt;span class="nb"&gt;grep &lt;/span&gt;Memory


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here's the output for me showing that the current usage is 5.5Gib.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

MemoryCurrent=5596528640
EffectiveMemoryNodes=
AllowedMemoryNodes=
MemoryAccounting=yes
DefaultMemoryLow=0
DefaultMemoryMin=0
MemoryMin=0
MemoryLow=0
MemoryHigh=6442450944
MemoryMax=7516192768
MemorySwapMax=0
MemoryLimit=infinity


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Load many tabs and observe &lt;code&gt;MemoryCurrent&lt;/code&gt; value. If at some point the browser freezes and you see that &lt;code&gt;MemoryCurrent&lt;/code&gt; is close to or above &lt;code&gt;MemoryHigh&lt;/code&gt;, you may need to tune settings. If things run smoothly all the time, consider decreasing &lt;code&gt;MemoryHigh&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Tune settings
&lt;/h2&gt;

&lt;p&gt;For me Firefox content processes each use 1.5-2.3Gb at peak load, and so &lt;code&gt;MemoryHigh&lt;/code&gt; value should be greater than &lt;code&gt;N*2.3Gb&lt;/code&gt; where &lt;code&gt;N&lt;/code&gt; is the number of content processes you've configured in browser settings.&lt;br&gt;
You also need to include some non-trivial amount for the parent Firefox process and other stuff that it spawns.&lt;br&gt;
If your machine is low on memory, consider decreasing &lt;code&gt;N&lt;/code&gt;, i.e. further decrease amount of content processes in the browser configuration.&lt;/p&gt;

</description>
      <category>firefox</category>
      <category>cgroups</category>
      <category>systemd</category>
      <category>memory</category>
    </item>
  </channel>
</rss>
