Bug 1190150: Decommit LFH caches when responding to memory pressure events; draft
authorAaron Klotz <aklotz@mozilla.com>
Wed, 05 Oct 2016 15:41:36 -0600
changeset 485449 ab434f725d023f7ee1e815b7e31ae0802c104418
parent 485448 edebc42baa270cd5b28c604de6368365cc45eefd
child 485450 40832b849691bf87dfb6566ec8142a5a72fce7f2
push id45733
push useraklotz@mozilla.com
push dateThu, 16 Feb 2017 17:45:58 +0000
bugs1190150
milestone54.0a1
Bug 1190150: Decommit LFH caches when responding to memory pressure events; MozReview-Commit-ID: GPr2qWBZz27
xpcom/base/AvailableMemoryTracker.cpp
--- a/xpcom/base/AvailableMemoryTracker.cpp
+++ b/xpcom/base/AvailableMemoryTracker.cpp
@@ -1,14 +1,28 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+// These macros must be redefined to be able to make the HeapSetInformation
+// call later in this file...
+#if (NTDDI_VERSION < 0x06030000)
+#undef NTDDI_VERSION
+#define NTDDI_VERSION 0x06030000
+#if !defined(WINBLUE_KBSPING14)
+#define WINBLUE_KBSPRING14
+#endif
+#endif
+#if (_WIN32_WINNT < 0x0603)
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0603
+#endif
+
 #include "mozilla/AvailableMemoryTracker.h"
 
 #if defined(XP_WIN)
 #include "prinrval.h"
 #include "prenv.h"
 #include "nsIMemoryReporter.h"
 #include "nsMemoryPressure.h"
 #endif
@@ -18,18 +32,20 @@
 #include "nsIRunnable.h"
 #include "nsISupports.h"
 #include "nsThreadUtils.h"
 
 #include "mozilla/Preferences.h"
 #include "mozilla/Services.h"
 
 #if defined(XP_WIN)
+#   include "mozilla/WindowsVersion.h"
 #   include "nsWindowsDllInterceptor.h"
 #   include <windows.h>
+#   include <objbase.h>
 #endif
 
 #if defined(MOZ_MEMORY)
 #   include "mozmemory.h"
 #endif  // MOZ_MEMORY
 
 using namespace mozilla;
 
@@ -303,16 +319,30 @@ NS_IMPL_ISUPPORTS(nsJemallocFreeDirtyPag
 NS_IMETHODIMP
 nsJemallocFreeDirtyPagesRunnable::Run()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
 #if defined(MOZ_MEMORY)
   jemalloc_free_dirty_pages();
 #endif
+#if defined(XP_WIN)
+  /* TODO ASK: We dont' call jemalloc_purge_freed_pages, but even if we did,
+     it just does a MEM_RESET, not a free, so GlobalMemoryStatus still won't
+     take that into account. It looks like the sanctioned way to do this
+     with our jemalloc wrapper is to call jemalloc_purge_freed_pages,
+     but that is only implemented on mac and is intended to be deprecated.*/
+  CoFreeUnusedLibraries();
+  if (IsWin8Point1OrLater()) {
+    HEAP_OPTIMIZE_RESOURCES_INFORMATION heapOptInfo =
+      {HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION};
+    HeapSetInformation(nullptr, HeapOptimizeResources, &heapOptInfo,
+                       sizeof(heapOptInfo));
+  }
+#endif
 
   return NS_OK;
 }
 
 /**
  * The memory pressure watcher is used for listening to memory-pressure events
  * and reacting upon them. We use one instance per process currently only for
  * cleaning up dirty unused pages held by jemalloc.