From 9488fae2a38cda734e4f14475b9c8e5ad25a6183 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 11 May 2011 16:09:50 -0700 Subject: [PATCH] Bug 654041 - Add buttons that trigger GC and CC from about:memory. r=vlad,jruderman. --- .../aboutmemory/content/aboutMemory.js | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/toolkit/components/aboutmemory/content/aboutMemory.js b/toolkit/components/aboutmemory/content/aboutMemory.js index f4f042882b8d..1efffff95988 100644 --- a/toolkit/components/aboutmemory/content/aboutMemory.js +++ b/toolkit/components/aboutmemory/content/aboutMemory.js @@ -40,6 +40,7 @@ const Cc = Components.classes; const Ci = Components.interfaces; +const Cu = Components.utils; // Must use .href here instead of .search because "about:memory" is a // non-standard URL. @@ -73,6 +74,49 @@ function $(n) return document.getElementById(n); } +function doGlobalGC() +{ + Cu.forceGC(); + update(); +} + +function doGlobalGCandCC() +{ + window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindowUtils) + .garbageCollect(); + update(); +} + +// For maximum effect, this returns to the event loop between each +// notification. See bug 610166 comment 12 for an explanation. +// Ideally a single notification would be enough. +function sendHeapMinNotifications() +{ + function runSoon(f) + { + var tm = Cc["@mozilla.org/thread-manager;1"] + .getService(Ci.nsIThreadManager); + + tm.mainThread.dispatch({ run: f }, Ci.nsIThread.DISPATCH_NORMAL); + } + + function sendHeapMinNotificationsInner() + { + var os = Cc["@mozilla.org/observer-service;1"] + .getService(Ci.nsIObserverService); + os.notifyObservers(null, "memory-pressure", "heap-minimize"); + + if (++j < 3) + runSoon(sendHeapMinNotificationsInner); + else + runSoon(update); + } + + var j = 0; + sendHeapMinNotificationsInner(); +} + /** * Top-level function that does the work of generating the page. */ @@ -143,6 +187,23 @@ function update() } } + // Memory-related actions. + const GCDesc = "Do a global garbage collection."; + // XXX: once bug 625302 is fixed, should change this button to just do a CC. + const CCDesc = "Do a global garbage collection followed by a cycle " + + "collection. (It currently is not possible to do a cycle " + + "collection on its own, see bug 625302.)"; + const MPDesc = "Send three \"heap-minimize\" notifications in a row. Each " + + "notification triggers a global garbage collection followed " + + "by a cycle collection, and causes the process to reduce " + + "memory usage in other ways, e.g. by flushing various caches."; + + text += "
" + + "" + + "" + + "" + + "
"; + // Generate verbosity option link at the bottom. text += gVerbose ? "Less verbose"