How to measure firefox add-on memory usage
Asked Answered
B

1

7


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?

Bulletproof answered 6/2, 2012 at 18:17 Comment(2)
not sure if it would help you at all... but you can type "about:memory" in the address bar, but I dont think it will get as granular as you are hoping for.Rogerrogerio
the about:memory is not helpful at all, In order to understand something in there I need to learn how firefox memory usage is working... and this is too much.Bulletproof
I
6

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.

Incrassate answered 7/2, 2012 at 7:26 Comment(3)
Thanks a lot, my code is running in sandbox, all I need now is just to add the names to my sandbox and then I can profile it, thank you!Bulletproof
@Yosy: See update to my answer, now you can look at the entire graph of JavaScript objects.Incrassate
Thanks Vladimir, thanks to the original answer I optimized my memory usage in my extension, I will check this extension out :)Bulletproof

© 2022 - 2024 — McMap. All rights reserved.