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

Side by Side Diff: base/memory/memory_pressure_listener.h

Issue 1749073002: Do V8 GC ASAP if system memory is pressured (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: based on CL 1813963002 Created 4 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // MemoryPressure provides static APIs for handling memory pressure on 5 // MemoryPressure provides static APIs for handling memory pressure on
6 // platforms that have such signals, such as Android and ChromeOS. 6 // platforms that have such signals, such as Android and ChromeOS.
7 // The app will try to discard buffers that aren't deemed essential (individual 7 // The app will try to discard buffers that aren't deemed essential (individual
8 // modules will implement their own policy). 8 // modules will implement their own policy).
9 9
10 #ifndef BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ 10 #ifndef BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_
11 #define BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ 11 #define BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_
12 12
13 #include "base/base_export.h" 13 #include "base/base_export.h"
14 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/lazy_instance.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/observer_list_threadsafe.h"
16 18
17 namespace base { 19 namespace base {
18 20
19 // To start listening, create a new instance, passing a callback to a 21 // To start listening, create a new instance, passing a callback to a
20 // function that takes a MemoryPressureLevel parameter. To stop listening, 22 // function that takes a MemoryPressureLevel parameter. To stop listening,
21 // simply delete the listener object. The implementation guarantees 23 // simply delete the listener object. The implementation guarantees
22 // that the callback will always be called on the thread that created 24 // that the callback will always be called on the thread that created
23 // the listener. 25 // the listener.
24 // Note that even on the same thread, the callback is not guaranteed to be 26 // Note that even on the same thread, the callback is not guaranteed to be
25 // called synchronously within the system memory pressure broadcast. 27 // called synchronously within the system memory pressure broadcast.
(...skipping 12 matching lines...) Expand all
38 // MemoryPressureListener* my_listener = 40 // MemoryPressureListener* my_listener =
39 // new MemoryPressureListener(base::Bind(&OnMemoryPressure)); 41 // new MemoryPressureListener(base::Bind(&OnMemoryPressure));
40 // 42 //
41 // ... 43 // ...
42 // 44 //
43 // // Stop listening. 45 // // Stop listening.
44 // delete my_listener; 46 // delete my_listener;
45 // 47 //
46 class BASE_EXPORT MemoryPressureListener { 48 class BASE_EXPORT MemoryPressureListener {
47 public: 49 public:
50 // ObserverListThreadSafe is RefCountedThreadSafe, this traits is needed
esprehn 2016/03/25 09:20:35 Why does this all need to be moved into the header
hong.zheng 2016/04/01 09:22:34 According to Mark's comments
51 // to ensure the LazyInstance will hold a reference to it.
52 struct LeakyLazyObserverListTraits :
53 base::internal::LeakyLazyInstanceTraits<
54 ObserverListThreadSafe<MemoryPressureListener> > {
55 static ObserverListThreadSafe<MemoryPressureListener>*
56 New(void* instance) {
57 ObserverListThreadSafe<MemoryPressureListener>* ret =
58 base::internal::LeakyLazyInstanceTraits<
59 ObserverListThreadSafe<MemoryPressureListener>>::New(instance);
60 // Leaky.
61 ret->AddRef();
62 return ret;
63 }
64 };
65
66 struct MemoryPressureListenerObservers {
67 LazyInstance<ObserverListThreadSafe<MemoryPressureListener>,
68 LeakyLazyObserverListTraits> observers_;
69 LazyInstance<ObserverList<MemoryPressureListener>> sync_observers_;
70 base::LazyInstance<base::Lock>::Leaky sync_observers_lock_;
71 };
72
48 // A Java counterpart will be generated for this enum. 73 // A Java counterpart will be generated for this enum.
49 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base 74 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base
50 enum MemoryPressureLevel { 75 enum MemoryPressureLevel {
51 // No problems, there is enough memory to use. This event is not sent via 76 // No problems, there is enough memory to use. This event is not sent via
52 // callback, but the enum is used in other places to find out the current 77 // callback, but the enum is used in other places to find out the current
53 // state of the system. 78 // state of the system.
54 MEMORY_PRESSURE_LEVEL_NONE = -1, 79 MEMORY_PRESSURE_LEVEL_NONE = -1,
55 80
56 // Modules are advised to free buffers that are cheap to re-allocate and not 81 // Modules are advised to free buffers that are cheap to re-allocate and not
57 // immediately needed. 82 // immediately needed.
58 MEMORY_PRESSURE_LEVEL_MODERATE = 0, 83 MEMORY_PRESSURE_LEVEL_MODERATE = 0,
59 84
60 // At this level, modules are advised to free all possible memory. The 85 // At this level, modules are advised to free all possible memory. The
61 // alternative is to be killed by the system, which means all memory will 86 // alternative is to be killed by the system, which means all memory will
62 // have to be re-created, plus the cost of a cold start. 87 // have to be re-created, plus the cost of a cold start.
63 MEMORY_PRESSURE_LEVEL_CRITICAL = 2, 88 MEMORY_PRESSURE_LEVEL_CRITICAL = 2,
64 }; 89 };
65 90
66 typedef base::Callback<void(MemoryPressureLevel)> MemoryPressureCallback; 91 typedef base::Callback<void(MemoryPressureLevel)> MemoryPressureCallback;
92 typedef base::Callback<void(MemoryPressureLevel)> SyncMemoryPressureCallback;
67 93
68 explicit MemoryPressureListener( 94 explicit MemoryPressureListener(
69 const MemoryPressureCallback& memory_pressure_callback); 95 const MemoryPressureCallback& memory_pressure_callback);
96 explicit MemoryPressureListener(
97 const MemoryPressureCallback& memory_pressure_callback,
98 const SyncMemoryPressureCallback& sync_memory_pressure_callback);
99
70 ~MemoryPressureListener(); 100 ~MemoryPressureListener();
71 101
72 // Intended for use by the platform specific implementation. 102 // Intended for use by the platform specific implementation.
73 static void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); 103 static void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level);
74 104
75 // These methods should not be used anywhere else but in memory measurement 105 // These methods should not be used anywhere else but in memory measurement
76 // code, where they are intended to maintain stable conditions across 106 // code, where they are intended to maintain stable conditions across
77 // measurements. 107 // measurements.
78 static bool AreNotificationsSuppressed(); 108 static bool AreNotificationsSuppressed();
79 static void SetNotificationsSuppressed(bool suppressed); 109 static void SetNotificationsSuppressed(bool suppressed);
80 static void SimulatePressureNotification( 110 static void SimulatePressureNotification(
81 MemoryPressureLevel memory_pressure_level); 111 MemoryPressureLevel memory_pressure_level);
82 112
83 private: 113 private:
84 void Notify(MemoryPressureLevel memory_pressure_level); 114 void Notify(MemoryPressureLevel memory_pressure_level);
115 void SyncNotify(MemoryPressureLevel memory_pressure_level);
85 116
86 static void DoNotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); 117 static void DoNotifyMemoryPressure(MemoryPressureLevel memory_pressure_level);
87 118
88 MemoryPressureCallback callback_; 119 MemoryPressureCallback callback_;
120 SyncMemoryPressureCallback sync_memory_pressure_callback_;
121
122 static MemoryPressureListenerObservers* g_observers;
esprehn 2016/03/25 09:20:35 why does this need to be moved into the header?
Mark Mentovai 2016/03/25 16:59:31 esprehn wrote:
hong.zheng 2016/04/01 09:22:34 I am sorry to misunderstand Mark's comments before
89 123
90 DISALLOW_COPY_AND_ASSIGN(MemoryPressureListener); 124 DISALLOW_COPY_AND_ASSIGN(MemoryPressureListener);
91 }; 125 };
92 126
93 } // namespace base 127 } // namespace base
94 128
95 #endif // BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ 129 #endif // BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/memory_pressure_listener.cc » ('j') | content/renderer/render_thread_impl.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698