Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2213)

Unified Diff: content/renderer/render_thread_impl.cc

Issue 1749073002: Do V8 GC ASAP if system memory is pressured (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/render_thread_impl.cc
diff --git a/content/renderer/render_thread_impl.cc b/content/renderer/render_thread_impl.cc
index fdba7c596ae48e0f141bcbae111a34a971994741..648ac5128c2e7591af92b4c70b3456027cd6088c 100644
--- a/content/renderer/render_thread_impl.cc
+++ b/content/renderer/render_thread_impl.cc
@@ -161,7 +161,6 @@
#include "third_party/skia/include/core/SkGraphics.h"
#include "ui/base/layout.h"
#include "ui/base/ui_base_switches.h"
-#include "v8/include/v8.h"
#if defined(OS_ANDROID)
#include <cpu-features.h>
@@ -242,6 +241,17 @@ const size_t kImageCacheSingleAllocationByteLimit = 64 * 1024 * 1024;
base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl> >
lazy_tls = LAZY_INSTANCE_INITIALIZER;
+// v8::MemoryPressureLevel should correspond to base::MemoryPressureListener.
+static_assert(static_cast<v8::MemoryPressureLevel>(
+ base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) ==
+ v8::MemoryPressureLevel::kNone, "none level not align");
+static_assert(static_cast<v8::MemoryPressureLevel>(
+ base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE) ==
+ v8::MemoryPressureLevel::kModerate, "moderate level not align");
+static_assert(static_cast<v8::MemoryPressureLevel>(
+ base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) ==
+ v8::MemoryPressureLevel::kCritical, "critical level not align");
+
class WebThreadForCompositor : public WebThreadImplForWorkerScheduler {
public:
explicit WebThreadForCompositor(base::Thread::Options options)
@@ -348,13 +358,6 @@ void NotifyTimezoneChangeOnThisThread() {
v8::Date::DateTimeConfigurationChangeNotification(isolate);
}
-void LowMemoryNotificationOnThisThread() {
- v8::Isolate* isolate = v8::Isolate::GetCurrent();
- if (!isolate)
- return;
- isolate->LowMemoryNotification();
-}
-
class RenderFrameSetupImpl : public mojom::RenderFrameSetup {
public:
explicit RenderFrameSetupImpl(
@@ -823,7 +826,9 @@ void RenderThreadImpl::Init(
#endif
memory_pressure_listener_.reset(new base::MemoryPressureListener(
- base::Bind(&RenderThreadImpl::OnMemoryPressure, base::Unretained(this))));
+ base::Bind(&RenderThreadImpl::OnMemoryPressure, base::Unretained(this)),
+ base::Bind(&RenderThreadImpl::OnSyncMemoryPressure,
+ base::Unretained(this))));
int num_raster_threads = 0;
std::string string_value =
@@ -1931,6 +1936,7 @@ void RenderThreadImpl::OnCreateNewSharedWorker(
void RenderThreadImpl::OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
+ TRACE_EVENT0("memory","RenderThreadImpl::OnMemoryPressure");
ReleaseFreeMemory();
// Do not call into blink if it is not initialized.
@@ -1938,19 +1944,6 @@ void RenderThreadImpl::OnMemoryPressure(
blink::WebMemoryPressureListener::onMemoryPressure(
static_cast<blink::WebMemoryPressureLevel>(memory_pressure_level));
- if (blink::mainThreadIsolate()) {
- // Trigger full v8 garbage collection on memory pressure notifications.
- // This will potentially hang the renderer for a long time, however, when
- // we receive a memory pressure notification, we might be about to be
- // killed. Because of the janky hang don't do this to foreground
- // renderers.
- if (RendererIsHidden()) {
- blink::mainThreadIsolate()->LowMemoryNotification();
- RenderThread::Get()->PostTaskToAllWebWorkers(
- base::Bind(&LowMemoryNotificationOnThisThread));
- }
- }
-
if (memory_pressure_level ==
base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
// Purge Skia font cache, by setting it to 0 and then again to the
@@ -2111,4 +2104,24 @@ void RenderThreadImpl::PendingRenderFrameConnect::OnConnectionError() {
DCHECK_EQ(1u, erased);
}
+void RenderThreadImpl::OnSyncMemoryPressure(
+ base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
+ if (!blink::mainThreadIsolate())
+ return;
+
+ v8::MemoryPressureLevel v8_memory_pressure_level =
+ static_cast<v8::MemoryPressureLevel>(memory_pressure_level);
+
+ // In order to reduce performance impact, translate critical level to
+ // moderate level for foregroud renderer.
+ if (!RendererIsHidden() &&
+ v8_memory_pressure_level == v8::MemoryPressureLevel::kCritical)
+ v8_memory_pressure_level = v8::MemoryPressureLevel::kModerate;
+
+ blink::mainThreadIsolate()->MemoryPressureNotification(
+ v8_memory_pressure_level);
+ blink::MemoryPressureNotificationToWorkerThreadIsolates(
+ v8_memory_pressure_level);
+}
+
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698