How to Measure Firefox Extra Memory Usage


I am developing a firefox add-on using XUL, and I want to measure and profile the use of my extension memory.
How can i do this? and check which function uses the most memory and how much memory is used by my add in firefox?

+7
source share
1 answer

You cannot measure the influence of a single function; memory management in Firefox does not work at this level - it works with bays. If your extension has its own window, you can see the compartment of this window under about:memory?verbose (click "Minimize memory usage", otherwise you can see objects that will be garbage collected there). If your extension code works in the context of a browser window, you are usually out of luck - you cannot distinguish it from other scripts running there. The same with XPCOM components and JavaScript modules - they are all loaded into the [System Principal] compartment.

What can you do to keep your scripts separate from the large compartment, however: use sandboxes, the sandbox always gets its own compartment. For example, in a browser window, you should 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, myExtension/browserScript.js appears in about:memory?verbose myExtension/browserScript.js , and you can see how much memory this script (along with the objects it creates, etc.) is executed accurately. What you need to remember:

  • The script in the sandbox will not have access to variables from the "outside". You must explicitly set these variables as sandbox properties (for example, I did with the window variable in the example).
  • Departments are not cheap, and passing objects between compartments is also not cheap. Therefore, creating one compartment for each function would be a bad idea due to the associated overhead.

Documentation: Sandbox , Services.jsm

Update . As for Firefox 13, everything has changed. There is this extension , for example, which will show you all the objects that are currently in memory. Still far from being comfortable, and getting the whole picture is nontrivial - but it gives you detail below the compartments.

+6
source

All Articles