Commit 9488fae2 authored by Nicholas Nethercote's avatar Nicholas Nethercote
Browse files

Bug 654041 - Add buttons that trigger GC and CC from about:memory. r=vlad,jruderman.

parent 95582933
Loading
Loading
Loading
Loading
+61 −0
Original line number Diff line number Diff line
@@ -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 += "<div>" +
    "<button title='" + GCDesc + "' onclick='doGlobalGC()'>GC</button>" +
    "<button title='" + CCDesc + "' onclick='doGlobalGCandCC()'>GC + CC</button>" +
    "<button title='" + MPDesc + "' onclick='sendHeapMinNotifications()'>" + "Minimize memory usage</button>" +
    "</div>";

  // Generate verbosity option link at the bottom.
  text += gVerbose
        ? "<span class='option'><a href='about:memory'>Less verbose</a></span>"