Bug 850607 - Properly react to changes to the 'memory.free_dirty_pages' preference. r=jlebar
authorGabriele Svelto <gsvelto@mozilla.com>
Wed, 13 Mar 2013 12:24:04 +0100
changeset 124705 acada241cc5d43207ef1f897b7a0aa2d7b8f3531
parent 124704 62a6fba29defa98ddc5ffe811b469fe11597ccd2
child 124706 53d3d33713c26015e3c0a0fa28200c1b10caac75
push id24433
push useremorley@mozilla.com
push dateThu, 14 Mar 2013 12:21:10 +0000
treeherdermozilla-central@96af92fa87fd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjlebar
bugs850607
milestone22.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 850607 - Properly react to changes to the 'memory.free_dirty_pages' preference. r=jlebar
xpcom/base/AvailableMemoryTracker.cpp
--- a/xpcom/base/AvailableMemoryTracker.cpp
+++ b/xpcom/base/AvailableMemoryTracker.cpp
@@ -512,49 +512,58 @@ nsJemallocFreeDirtyPagesRunnable::Run()
  */
 class nsMemoryPressureWatcher MOZ_FINAL : public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 
   void Init();
+
+private:
+  static bool sFreeDirtyPages;
 };
 
 NS_IMPL_ISUPPORTS1(nsMemoryPressureWatcher, nsIObserver)
 
+bool nsMemoryPressureWatcher::sFreeDirtyPages = false;
+
 /**
  * Initialize and subscribe to the memory-pressure events. We subscribe to the
  * observer service in this method and not in the constructor because we need
  * to hold a strong reference to 'this' before calling the observer service.
  */
 void
 nsMemoryPressureWatcher::Init()
 {
   nsCOMPtr<nsIObserverService> os = services::GetObserverService();
 
   if (os) {
     os->AddObserver(this, "memory-pressure", /* ownsWeak */ false);
   }
+
+  Preferences::AddBoolVarCache(&sFreeDirtyPages, "memory.free_dirty_pages",
+                               false);
 }
 
 /**
  * Reacts to all types of memory-pressure events, launches a runnable to
  * free dirty pages held by jemalloc.
- * @see nsMemoryPressureWatcher::FreeDirtyPages
  */
 NS_IMETHODIMP
 nsMemoryPressureWatcher::Observe(nsISupports *subject, const char *topic,
                                  const PRUnichar *data)
 {
   MOZ_ASSERT(!strcmp(topic, "memory-pressure"), "Unknown topic");
 
-  nsRefPtr<nsIRunnable> runnable = new nsJemallocFreeDirtyPagesRunnable();
+  if (sFreeDirtyPages) {
+    nsRefPtr<nsIRunnable> runnable = new nsJemallocFreeDirtyPagesRunnable();
 
-  NS_DispatchToMainThread(runnable);
+    NS_DispatchToMainThread(runnable);
+  }
 
   return NS_OK;
 }
 
 } // anonymous namespace
 
 namespace mozilla {
 namespace AvailableMemoryTracker {
@@ -585,21 +594,19 @@ void Activate()
   NS_RegisterMemoryReporter(new NumLowCommitSpaceEventsMemoryReporter());
   NS_RegisterMemoryReporter(new NumLowPhysicalMemoryEventsMemoryReporter());
   if (sizeof(void*) == 4) {
     NS_RegisterMemoryReporter(new NumLowVirtualMemoryEventsMemoryReporter());
   }
   sHooksActive = true;
 #endif
 
-  if (Preferences::GetBool("memory.free_dirty_pages", false)) {
-    // This object is held alive by the observer service.
-    nsRefPtr<nsMemoryPressureWatcher> watcher = new nsMemoryPressureWatcher();
-    watcher->Init();
-  }
+  // This object is held alive by the observer service.
+  nsRefPtr<nsMemoryPressureWatcher> watcher = new nsMemoryPressureWatcher();
+  watcher->Init();
 }
 
 void Init()
 {
   // Do nothing on x86-64, because nsWindowsDllInterceptor is not thread-safe
   // on 64-bit.  (On 32-bit, it's probably thread-safe.)  Even if we run Init()
   // before any other of our threads are running, another process may have
   // started a remote thread which could call VirtualAlloc!