{"id":455,"date":"2025-05-28T12:17:35","date_gmt":"2025-05-28T12:17:35","guid":{"rendered":"https:\/\/blog.mozilla.org\/data\/?p=455"},"modified":"2025-05-28T12:17:35","modified_gmt":"2025-05-28T12:17:35","slug":"glean-memory-usage-reporting","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/data\/2025\/05\/28\/glean-memory-usage-reporting\/","title":{"rendered":"Glean Memory Usage Reporting"},"content":{"rendered":"<p>Since <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=1896609\">Bug 1896609<\/a> landed we now have <a href=\"https:\/\/github.com\/mozilla\/glean\">Glean<\/a> &amp; <a href=\"https:\/\/firefox-source-docs.mozilla.org\/toolkit\/components\/glean\/index.html\">Firefox on Glean (FOG)<\/a> memory reporting built into the Firefox Memory Reporter. This allows us to measure the allocated memory in use by Glean and FOG. It currently covers memory allocated by the C++ module of FOG and all instantiated Glean metrics. It does not yet measure the memory used by Glean and its database.<\/p>\n<h2>How it works<\/h2>\n<p>Firefox has a built-in memory usage reporter, available as <a href=\"https:\/\/firefox-source-docs.mozilla.org\/performance\/memory\/about_colon_memory.html\"><code>about:memory<\/code><\/a>. Components of Firefox can expose their own memory usage by implementing the <a href=\"https:\/\/searchfox.org\/mozilla-central\/source\/xpcom\/base\/nsIMemoryReporter.idl\"><code>nsIMemoryReporter<\/code><\/a> interface. FOG implements this interface and delegates the measurement to the <code>firefox-on-glean<\/code> Rust component.<\/p>\n<p><code>firefox-on-glean<\/code> then collects the memory usage of objects under its own control: all user-defined and runtime-instantiated metrics, additional hashmaps used to track metrics &amp; all user-defined and runtime-instantiated pings. It will soon also collect the memory size of the global Glean object, and thus the memory used for built-in metrics as well as the in-memory database.<\/p>\n<p>Memory measurement works by following all heap-allocated pointers, asking the allocator for the memory size of each and summing everything up. Because we do most of this measurement in Rust we use the existing <a href=\"https:\/\/crates.io\/crates\/wr_malloc_size_of\">wr_malloc_size_of<\/a> crate, which already implements the correct measurement for most Rust libstd types as well as some additional library-provided types. Our own types implement the required trait using <a href=\"https:\/\/crates.io\/crates\/malloc_size_of_derive\">malloc_size_of_derive<\/a> for automatically deriving the trait, or manual implementations.<\/p>\n<h2>How it looks<\/h2>\n<p>The memory measurement is built into Firefox and works in every shipped build. Open up <code>about:memory<\/code> in a running Firefox, click the \u201cMeasure\u201d button and wait for the measurement. Once all data is collected it will show a long tree of measured allocations across all processes. Type <code>fog<\/code> into the filter box on the right to trim it down to only allocations from the fog component. The exact numbers differ between runs and operating systems.<\/p>\n<p>You will see a view similar to this:<\/p>\n<div id=\"attachment_456\" style=\"width: 1301px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-456\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/blog.mozilla.org\/data\/files\/2025\/05\/screenshot-open-initial.png\" alt=\"&lt;p&gt;&lt;em&gt;about:memory on a freshly launched developer build of Firefox. fog reports 0.35 MB of allocated memory in the main process.&lt;\/em&gt;&lt;\/p&gt;\" width=\"1291\" height=\"985\" class=\"size-full wp-image-456\" srcset=\"https:\/\/blog.mozilla.org\/data\/files\/2025\/05\/screenshot-open-initial.png 1291w, https:\/\/blog.mozilla.org\/data\/files\/2025\/05\/screenshot-open-initial-300x229.png 300w, https:\/\/blog.mozilla.org\/data\/files\/2025\/05\/screenshot-open-initial-600x458.png 600w, https:\/\/blog.mozilla.org\/data\/files\/2025\/05\/screenshot-open-initial-768x586.png 768w, https:\/\/blog.mozilla.org\/data\/files\/2025\/05\/screenshot-open-initial-1000x763.png 1000w\" sizes=\"(max-width: 1291px) 100vw, 1291px\" \/><p id=\"caption-attachment-456\" class=\"wp-caption-text\"><\/p>\n<p><em>about:memory on a freshly launched developer build of Firefox. fog reports 0.35 MB of allocated memory in the main process.<\/em><\/p>\n<p><\/p><\/div>\n<p>After opening a few tabs and browsing the web a new measurement on <code>about:memory<\/code> will show a different number, as Glean is instantiating more metrics and therefore allocating more memory. This number will grow as more metrics are instantiated and kept in memory.<\/p>\n<p>This currently does not show the allocations from the global Glean object and its in-memory database. In the future we will be able to measure those allocations as well. In a prototype locally this already works as expected: As more data is recorded and stored the allocated memory grows. Once a ping is assembled, submitted and sent the allocations will be freed and <code>about:memory<\/code> will report less memory allocated again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since Bug 1896609 landed we now have Glean &amp; Firefox on Glean (FOG) memory reporting built into the Firefox Memory Reporter. This allows us to measure the allocated memory in &hellip; <a class=\"go\" href=\"https:\/\/blog.mozilla.org\/data\/2025\/05\/28\/glean-memory-usage-reporting\/\">Read more<\/a><\/p>\n","protected":false},"author":1756,"featured_media":422,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[315988],"tags":[525,448297,448351],"coauthors":[448350],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/posts\/455"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/users\/1756"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/comments?post=455"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/posts\/455\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/media\/422"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/media?parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/categories?post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/tags?post=455"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/coauthors?post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}