bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // MemoryPressure provides static APIs for handling memory pressure on |
skuhne | 4bf947f | 2014-12-17 09:01:49 +0900 | [diff] [blame] | 6 | // platforms that have such signals, such as Android and ChromeOS. |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 7 | // The app will try to discard buffers that aren't deemed essential (individual |
| 8 | // modules will implement their own policy). |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 9 | |
danakj | 0393da3 | 2015-03-10 09:31:16 +0900 | [diff] [blame] | 10 | #ifndef BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ |
| 11 | #define BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 12 | |
| 13 | #include "base/base_export.h" |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 14 | #include "base/callback.h" |
avi | 6751149 | 2015-12-24 17:44:47 +0900 | [diff] [blame] | 15 | #include "base/macros.h" |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | // To start listening, create a new instance, passing a callback to a |
| 20 | // function that takes a MemoryPressureLevel parameter. To stop listening, |
| 21 | // simply delete the listener object. The implementation guarantees |
| 22 | // that the callback will always be called on the thread that created |
| 23 | // the listener. |
boliu@chromium.org | fe8a13b | 2013-10-03 02:57:09 +0900 | [diff] [blame] | 24 | // Note that even on the same thread, the callback is not guaranteed to be |
| 25 | // called synchronously within the system memory pressure broadcast. |
skuhne | 4bf947f | 2014-12-17 09:01:49 +0900 | [diff] [blame] | 26 | // Please see notes in MemoryPressureLevel enum below: some levels are |
| 27 | // absolutely critical, and if not enough memory is returned to the system, |
| 28 | // it'll potentially kill the app, and then later the app will have to be |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 29 | // cold-started. |
| 30 | // |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 31 | // Example: |
| 32 | // |
| 33 | // void OnMemoryPressure(MemoryPressureLevel memory_pressure_level) { |
| 34 | // ... |
| 35 | // } |
| 36 | // |
| 37 | // // Start listening. |
| 38 | // MemoryPressureListener* my_listener = |
| 39 | // new MemoryPressureListener(base::Bind(&OnMemoryPressure)); |
| 40 | // |
| 41 | // ... |
| 42 | // |
| 43 | // // Stop listening. |
| 44 | // delete my_listener; |
| 45 | // |
| 46 | class BASE_EXPORT MemoryPressureListener { |
| 47 | public: |
mkosiba | d1fb9d4 | 2014-10-23 22:56:12 +0900 | [diff] [blame] | 48 | // A Java counterpart will be generated for this enum. |
| 49 | // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 50 | enum MemoryPressureLevel { |
skuhne | 4bf947f | 2014-12-17 09:01:49 +0900 | [diff] [blame] | 51 | // 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 |
| 53 | // state of the system. |
hong.zheng | 5996d39 | 2016-05-11 11:33:39 +0900 | [diff] [blame] | 54 | MEMORY_PRESSURE_LEVEL_NONE, |
skuhne | 4bf947f | 2014-12-17 09:01:49 +0900 | [diff] [blame] | 55 | |
mkosiba | d1fb9d4 | 2014-10-23 22:56:12 +0900 | [diff] [blame] | 56 | // Modules are advised to free buffers that are cheap to re-allocate and not |
| 57 | // immediately needed. |
hong.zheng | 5996d39 | 2016-05-11 11:33:39 +0900 | [diff] [blame] | 58 | MEMORY_PRESSURE_LEVEL_MODERATE, |
mkosiba | d1fb9d4 | 2014-10-23 22:56:12 +0900 | [diff] [blame] | 59 | |
| 60 | // 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 |
| 62 | // have to be re-created, plus the cost of a cold start. |
hong.zheng | 5996d39 | 2016-05-11 11:33:39 +0900 | [diff] [blame] | 63 | MEMORY_PRESSURE_LEVEL_CRITICAL, |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 64 | }; |
| 65 | |
hong.zheng | 5996d39 | 2016-05-11 11:33:39 +0900 | [diff] [blame] | 66 | typedef Callback<void(MemoryPressureLevel)> MemoryPressureCallback; |
| 67 | typedef Callback<void(MemoryPressureLevel)> SyncMemoryPressureCallback; |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 68 | |
| 69 | explicit MemoryPressureListener( |
| 70 | const MemoryPressureCallback& memory_pressure_callback); |
hong.zheng | 5996d39 | 2016-05-11 11:33:39 +0900 | [diff] [blame] | 71 | MemoryPressureListener( |
| 72 | const MemoryPressureCallback& memory_pressure_callback, |
| 73 | const SyncMemoryPressureCallback& sync_memory_pressure_callback); |
| 74 | |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 75 | ~MemoryPressureListener(); |
| 76 | |
| 77 | // Intended for use by the platform specific implementation. |
| 78 | static void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); |
| 79 | |
petrcermak | 595fa8f | 2015-09-07 20:25:31 +0900 | [diff] [blame] | 80 | // These methods should not be used anywhere else but in memory measurement |
| 81 | // code, where they are intended to maintain stable conditions across |
| 82 | // measurements. |
| 83 | static bool AreNotificationsSuppressed(); |
| 84 | static void SetNotificationsSuppressed(bool suppressed); |
petrcermak | b7fd0b0 | 2015-09-28 22:05:18 +0900 | [diff] [blame] | 85 | static void SimulatePressureNotification( |
| 86 | MemoryPressureLevel memory_pressure_level); |
petrcermak | 595fa8f | 2015-09-07 20:25:31 +0900 | [diff] [blame] | 87 | |
vitalybuka | 3e6a4a8 | 2016-05-05 05:50:48 +0900 | [diff] [blame] | 88 | void Notify(MemoryPressureLevel memory_pressure_level); |
hong.zheng | 5996d39 | 2016-05-11 11:33:39 +0900 | [diff] [blame] | 89 | void SyncNotify(MemoryPressureLevel memory_pressure_level); |
vitalybuka | 3e6a4a8 | 2016-05-05 05:50:48 +0900 | [diff] [blame] | 90 | |
hong.zheng | 5996d39 | 2016-05-11 11:33:39 +0900 | [diff] [blame] | 91 | private: |
petrcermak | b7fd0b0 | 2015-09-28 22:05:18 +0900 | [diff] [blame] | 92 | static void DoNotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); |
| 93 | |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 94 | MemoryPressureCallback callback_; |
hong.zheng | 5996d39 | 2016-05-11 11:33:39 +0900 | [diff] [blame] | 95 | SyncMemoryPressureCallback sync_memory_pressure_callback_; |
bulach@chromium.org | a49afdc | 2013-06-13 00:33:47 +0900 | [diff] [blame] | 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(MemoryPressureListener); |
| 98 | }; |
| 99 | |
| 100 | } // namespace base |
| 101 | |
danakj | 0393da3 | 2015-03-10 09:31:16 +0900 | [diff] [blame] | 102 | #endif // BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ |