Skip to content

Instantly share code, notes, and snippets.

@jsantell
Created October 21, 2013 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsantell/7091737 to your computer and use it in GitHub Desktop.
Save jsantell/7091737 to your computer and use it in GitHub Desktop.
Window memory leak
/**
* Opens/closes windows using either Jetpack wrappers (`USE_JETPACK`) or using the `windowWatcher`.
* Either way, does not reclaim memory in:
* explicit -> window-objects -> top(none)/detached/window([system])
*/
let { Cc, Ci } = require('chrome');
let WW = Cc['@mozilla.org/embedcomp/window-watcher;1'].getService(Ci.nsIWindowWatcher);
let { open } = require('sdk/window/utils');
let { setInterval, clearInterval } = require('sdk/timers');
let URI = 'chrome://browser/content/browser.xul';
let INTERVAL = 100;
let count = 0;
let MAX = 100;
let USE_JETPACK = 0;
let lastWindow;
let timer = setInterval(() => {
if (lastWindow) lastWindow.close();
lastWindow = openWindow();
console.log("Opening " + count);
if (++count > MAX) clearInterval(timer);
}, INTERVAL);
function openWindow () {
if (USE_JETPACK)
return open(URI, {
name : 'simple addon',
features: {
width : 100,
height : 100,
chrome : true,
left : 100,
top : 100
}
});
else
return WW.openWindow(null, URI, 'a simple addon', 'width=100,height=100,chrome,left=100,top=100', null);
}
/**
* Open/closes windows using `windowWatcher`
*
* Reclaims memory in
* explicit -> window-objects -> top(none)/detached/window([system])
*/
let WW = Components.classes['@mozilla.org/embedcomp/window-watcher;1'].getService(Components.interfaces.nsIWindowWatcher);
let URI = 'chrome://browser/content/browser.xul';
let INTERVAL = 100;
let count = 0;
let MAX = 100;
let lastWindow;
let timer = setInterval(() => {
close();
console.log(count);
if (++count > MAX) clearInterval(timer);
open();
}, INTERVAL);
function open () {
lastWindow = WW.openWindow(null, URI, 'a simple addon', 'width=100,height=100,chrome,left=100,top=100', null);
}
function close () {
if (lastWindow) lastWindow.close();
}
@Gozala
Copy link

Gozala commented Nov 11, 2013

This does not seems to leak. So maybe it's window/utils ??

const { Cc, Ci } = require("chrome");
const { openWindow } = Cc['@mozilla.org/embedcomp/window-watcher;1'].
                       getService(Ci.nsIWindowWatcher);
const { setInterval, clearInterval } = require("sdk/timers");
const { open: openWin } = require('sdk/window/utils');
const URI = 'chrome://browser/content/browser.xul';
const INTERVAL = 100;
const MAX = 100;
const USE_JETPACK = true;

let count = 0;
let lastWindow;

const timer = setInterval(() => {
  close();
  console.log(count);
  if (++count > MAX)
    clearInterval(timer);
  open();
}, INTERVAL);


const open = () => {
  if (USE_JETPACK) {
    lastWindow = openWin(URI, {
      name : 'simple addon',
      features: {
        width : 100,
        height : 100,
        chrome : true,
        left : 100,
        top : 100
      }
    });
  }
  else {
    lastWindow = openWindow(null, URI, 'a simple addon', 'width=100,height=100,chrome,left=100,top=100', null);
  }
}

const close = () => {
  if (lastWindow) lastWindow.close();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment