Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure firefox add-on memory usage


I am developing a firefox add-on using XUL, and I want to measure and profile my extension memory usage.
How can I do this? and check which function is taking the most memory usage and how much memory usage my extension is adding to firefox?

like image 977
Yosi Avatar asked Feb 06 '12 18:02

Yosi


1 Answers

You cannot measure the impact of a single function, the memory management in Firefox doesn't work at this level - it works with compartments. If your extension has its own window then you will be able to see the compartment of this window under about:memory?verbose (click "Minimize memory usage", otherwise you might see objects there that will be garbage collected anyway). If your extension's code runs in the context of the browser window then you are usually out of luck - you will not be able to distinguish it from the other scripts running there. It's the same with XPCOM components and JavaScript modules - all of them get loaded into the "[System Principal]" compartment.

What you can do to get your scripts separated from a large compartment however: use sandboxes, a sandbox always gets its own compartment. For example, in a browser window you would do something like this:

Components.utils.import("resource://gre/modules/Services.jsm");
var mySandbox = Components.utils.Sandbox(window,
                  {sandboxName: "myExtension/browserScript.js"});
mySandbox.window = window; // Expose window variable to scripts in the sandbox
Services.scriptloader.loadSubScript("chrome://myextension/content/browserScript.js",
                                    mySandbox);
mySandbox.init();  // Call function init() of the script in the sandbox

As a result, a compartment called myExtension/browserScript.js will be displayed under about:memory?verbose and you will be able to see how much memory this script (along with objects it creates etc.) takes exactly. Things to keep in mind:

  • The script in the sandbox won't have access to the variables from "outside". You have to explicitly set these variables as properties of the sandbox (like I've done with the window variable in the example).
  • Compartments aren't cheap, and passing objects between compartments isn't cheap either. So creating one compartment for each function would be a bad idea because of the overheads involved.

Documentation: Sandbox, Services.jsm

Update: As of Firefox 13 things changed. There is this extension for example that will show you all the objects currently in memory. Still far from being comfortable, also getting the whole picture is non-trivial - but it gives you granularity on a level below compartments.

like image 126
Wladimir Palant Avatar answered Oct 07 '22 02:10

Wladimir Palant