Skip to main content

Garbage Collection improvements in Firefox improve memory usage

Mozilla introduced Garbage Collection in Firefox 38 that improves memory management of the web browser as part of the organization's ongoing effort to bring memory usage of the browser down and optimize it as best as possible for all use cases.

Compacting is one feature of the garbage collector that is currently only supporting JavaScript objects. It dropped memory usage on a test system -- the lead developers own - with 50 tabs open by about 2%. While that may not look like much, as it dropped usage on the system by about 30 Megabyte only to 1300 Megabyte total, it is just the first step with others to follow which will bring down memory usage further.

So how does compacting work in Firefox?

The JavaScript Heap uses arenas, 4K blocks of memory which are made up of cells that have fixed sizes. Each arena contains cells of the same kind and size only, and can only be freed if there are not any live cells in it anymore.

Memory problems arise when many arenas with few live cells each exist as the space cannot be released to memory in this case, While "dead" cell space can be used by the same kind, they cannot be used by different kinds.

Mozilla's algorithm works by moving live cells between arenas to reduce the number of arenas used by the Heap. It works by selecting appropriate cells, moving them and updating all pointers to make sure no objects or data is lost in the process.

The selection process lists all arenas with live cells in decreasing order and splits it in half to move cells out of arenas with few cells to release them to memory afterwards.

It is interesting to note that compacting triggers only on three specific events currently:

  1. When Firefox is running out of memory.
  2. When the operating system sends a "memory pressure" event.
  3. When the user has been inactive for a certain period of time (currently about 20 seconds)

Check how much memory free cells use

firefox heap

You can check how much memory cells use in Firefox.

  1. Type about:memory in the browser's address bar and hit enter.
  2. Search for js-main-runtime-gc-heap-committed
  3. Locate gc-things in its tree.

Additional information about the process are available on Mozilla Hacks.

Closing Words

There is a imbalance currently in the reporting that covers Firefox and Mozilla. Feature removals and additions get lots of publicity, usually by Firefox users opposing the change, whereas core improvements like these, that are mostly under the hood, get little to no publicity at all.

Mozilla could do a better job at promoting those under the hood improvements as those are features that are -- usually - welcome by all users of the browser.

This article was first seen on ComTek's "TekBits" Technology News

HOME