Title: [276619] trunk/Source
Revision
276619
Author
n...@apple.com
Date
2021-04-26 16:42:01 -0700 (Mon, 26 Apr 2021)

Log Message

Reduce memory footprint for background tabs
https://bugs.webkit.org/show_bug.cgi?id=225007

Reviewed by Chris Dumez.

When a WebContent process contains only non-visible pages (e.g. if it is a background tab),
we should attempt to reduce our memory footprint after some time interval to help relieve
system-wide memory pressure. This is enabled only on Mac because iOS already does something
similar just before WebContent suspends.

Source/WebKit:

* WebProcess/WebProcess.cpp:
(WebKit::WebProcess::pageDidEnterWindow):
(WebKit::WebProcess::pageWillLeaveWindow):
(WebKit::WebProcess::nonVisibleProcessGraphicsCleanupTimerFired):
(WebKit::WebProcess::nonVisibleProcessMemoryCleanupTimerFired):
(WebKit::WebProcess::nonVisibleProcessCleanupTimerFired): Deleted.
* WebProcess/WebProcess.h:

Source/WTF:

* wtf/PlatformEnableCocoa.h:

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (276618 => 276619)


--- trunk/Source/WTF/ChangeLog	2021-04-26 23:04:15 UTC (rev 276618)
+++ trunk/Source/WTF/ChangeLog	2021-04-26 23:42:01 UTC (rev 276619)
@@ -1,3 +1,17 @@
+2021-04-26  Ben Nham  <n...@apple.com>
+
+        Reduce memory footprint for background tabs
+        https://bugs.webkit.org/show_bug.cgi?id=225007
+
+        Reviewed by Chris Dumez.
+
+        When a WebContent process contains only non-visible pages (e.g. if it is a background tab),
+        we should attempt to reduce our memory footprint after some time interval to help relieve
+        system-wide memory pressure. This is enabled only on Mac because iOS already does something
+        similar just before WebContent suspends.
+
+        * wtf/PlatformEnableCocoa.h:
+
 2021-04-26  Keith Miller  <keith_mil...@apple.com>
 
         CodeBlock should do a better job accounting for extra memory it allocates.

Modified: trunk/Source/WTF/wtf/PlatformEnableCocoa.h (276618 => 276619)


--- trunk/Source/WTF/wtf/PlatformEnableCocoa.h	2021-04-26 23:04:15 UTC (rev 276618)
+++ trunk/Source/WTF/wtf/PlatformEnableCocoa.h	2021-04-26 23:42:01 UTC (rev 276619)
@@ -390,6 +390,10 @@
 #define ENABLE_NETWORK_CACHE_STALE_WHILE_REVALIDATE 1
 #endif
 
+#if !defined(ENABLE_NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER) && (PLATFORM(MAC) || PLATFORM(MACCATALYST))
+#define ENABLE_NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER 1
+#endif
+
 #if !defined(ENABLE_NOTIFICATIONS) && PLATFORM(MAC)
 #define ENABLE_NOTIFICATIONS 1
 #endif

Modified: trunk/Source/WebKit/ChangeLog (276618 => 276619)


--- trunk/Source/WebKit/ChangeLog	2021-04-26 23:04:15 UTC (rev 276618)
+++ trunk/Source/WebKit/ChangeLog	2021-04-26 23:42:01 UTC (rev 276619)
@@ -1,5 +1,25 @@
 2021-04-26  Ben Nham  <n...@apple.com>
 
+        Reduce memory footprint for background tabs
+        https://bugs.webkit.org/show_bug.cgi?id=225007
+
+        Reviewed by Chris Dumez.
+
+        When a WebContent process contains only non-visible pages (e.g. if it is a background tab),
+        we should attempt to reduce our memory footprint after some time interval to help relieve
+        system-wide memory pressure. This is enabled only on Mac because iOS already does something
+        similar just before WebContent suspends.
+
+        * WebProcess/WebProcess.cpp:
+        (WebKit::WebProcess::pageDidEnterWindow):
+        (WebKit::WebProcess::pageWillLeaveWindow):
+        (WebKit::WebProcess::nonVisibleProcessGraphicsCleanupTimerFired):
+        (WebKit::WebProcess::nonVisibleProcessMemoryCleanupTimerFired):
+        (WebKit::WebProcess::nonVisibleProcessCleanupTimerFired): Deleted.
+        * WebProcess/WebProcess.h:
+
+2021-04-26  Ben Nham  <n...@apple.com>
+
         Handle warning-level memory notifications more aggressively
         https://bugs.webkit.org/show_bug.cgi?id=225008
 

Modified: trunk/Source/WebKit/WebProcess/WebProcess.cpp (276618 => 276619)


--- trunk/Source/WebKit/WebProcess/WebProcess.cpp	2021-04-26 23:04:15 UTC (rev 276618)
+++ trunk/Source/WebKit/WebProcess/WebProcess.cpp	2021-04-26 23:42:01 UTC (rev 276619)
@@ -230,8 +230,14 @@
 static const Seconds plugInAutoStartExpirationTimeUpdateThreshold { 29 * 24 * 60 * 60 };
 
 // This should be greater than tileRevalidationTimeout in TileController.
-static const Seconds nonVisibleProcessCleanupDelay { 10_s };
+static const Seconds nonVisibleProcessGraphicsCleanupDelay { 10_s };
 
+#if ENABLE(NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER)
+// This should be long enough to support a workload where a user is actively switching between multiple tabs,
+// since our memory cleanup routine could potentially delete a good amount of JIT code.
+static const Seconds nonVisibleProcessMemoryCleanupDelay { 120_s };
+#endif
+
 namespace WebKit {
 using namespace JSC;
 using namespace WebCore;
@@ -265,7 +271,10 @@
 #if ENABLE(NETSCAPE_PLUGIN_API)
     , m_pluginProcessConnectionManager(PluginProcessConnectionManager::create())
 #endif
-    , m_nonVisibleProcessCleanupTimer(*this, &WebProcess::nonVisibleProcessCleanupTimerFired)
+    , m_nonVisibleProcessGraphicsCleanupTimer(*this, &WebProcess::nonVisibleProcessGraphicsCleanupTimerFired)
+#if ENABLE(NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER)
+    , m_nonVisibleProcessMemoryCleanupTimer(*this, &WebProcess::nonVisibleProcessMemoryCleanupTimerFired)
+#endif
 #if PLATFORM(IOS_FAMILY)
     , m_webSQLiteDatabaseTracker([this](bool isHoldingLockedFiles) { parentProcessConnection()->send(Messages::WebProcessProxy::SetIsHoldingLockedFiles(isHoldingLockedFiles), 0); })
 #endif
@@ -1554,7 +1563,11 @@
 void WebProcess::pageDidEnterWindow(PageIdentifier pageID)
 {
     m_pagesInWindows.add(pageID);
-    m_nonVisibleProcessCleanupTimer.stop();
+    m_nonVisibleProcessGraphicsCleanupTimer.stop();
+
+#if ENABLE(NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER)
+    m_nonVisibleProcessMemoryCleanupTimer.stop();
+#endif
 }
 
 void WebProcess::pageWillLeaveWindow(PageIdentifier pageID)
@@ -1561,11 +1574,18 @@
 {
     m_pagesInWindows.remove(pageID);
 
-    if (m_pagesInWindows.isEmpty() && !m_nonVisibleProcessCleanupTimer.isActive())
-        m_nonVisibleProcessCleanupTimer.startOneShot(nonVisibleProcessCleanupDelay);
+    if (m_pagesInWindows.isEmpty()) {
+        if (!m_nonVisibleProcessGraphicsCleanupTimer.isActive())
+            m_nonVisibleProcessGraphicsCleanupTimer.startOneShot(nonVisibleProcessGraphicsCleanupDelay);
+
+#if ENABLE(NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER)
+        if (!m_nonVisibleProcessMemoryCleanupTimer.isActive())
+            m_nonVisibleProcessMemoryCleanupTimer.startOneShot(nonVisibleProcessMemoryCleanupDelay);
+#endif
+    }
 }
     
-void WebProcess::nonVisibleProcessCleanupTimerFired()
+void WebProcess::nonVisibleProcessGraphicsCleanupTimerFired()
 {
     ASSERT(m_pagesInWindows.isEmpty());
     if (!m_pagesInWindows.isEmpty())
@@ -1576,6 +1596,19 @@
 #endif
 }
 
+#if ENABLE(NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER)
+void WebProcess::nonVisibleProcessMemoryCleanupTimerFired()
+{
+    ASSERT(m_pagesInWindows.isEmpty());
+    if (!m_pagesInWindows.isEmpty())
+        return;
+
+    WebCore::releaseMemory(Critical::Yes, Synchronous::No, WebCore::MaintainBackForwardCache::Yes, WebCore::MaintainMemoryCache::No);
+    for (auto& page : m_pageMap.values())
+        page->releaseMemory(Critical::Yes);
+}
+#endif
+
 void WebProcess::registerStorageAreaMap(StorageAreaMap& storageAreaMap)
 {
     ASSERT(storageAreaMap.identifier());

Modified: trunk/Source/WebKit/WebProcess/WebProcess.h (276618 => 276619)


--- trunk/Source/WebKit/WebProcess/WebProcess.h	2021-04-26 23:04:15 UTC (rev 276618)
+++ trunk/Source/WebKit/WebProcess/WebProcess.h	2021-04-26 23:42:01 UTC (rev 276619)
@@ -265,8 +265,12 @@
     void pageDidEnterWindow(WebCore::PageIdentifier);
     void pageWillLeaveWindow(WebCore::PageIdentifier);
 
-    void nonVisibleProcessCleanupTimerFired();
+    void nonVisibleProcessGraphicsCleanupTimerFired();
 
+#if ENABLE(NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER)
+    void nonVisibleProcessMemoryCleanupTimerFired();
+#endif
+
     void registerStorageAreaMap(StorageAreaMap&);
     void unregisterStorageAreaMap(StorageAreaMap&);
     StorageAreaMap* storageAreaMap(StorageAreaIdentifier) const;
@@ -662,8 +666,12 @@
     bool m_processIsSuspended { false };
 
     HashSet<WebCore::PageIdentifier> m_pagesInWindows;
-    WebCore::Timer m_nonVisibleProcessCleanupTimer;
+    WebCore::Timer m_nonVisibleProcessGraphicsCleanupTimer;
 
+#if ENABLE(NON_VISIBLE_WEBPROCESS_MEMORY_CLEANUP_TIMER)
+    WebCore::Timer m_nonVisibleProcessMemoryCleanupTimer;
+#endif
+
     RefPtr<WebCore::ApplicationCacheStorage> m_applicationCacheStorage;
 
 #if PLATFORM(IOS_FAMILY)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to