blob: 7e97010085dc8dcaea7f0fb68993b968fc5484c5 [file] [log] [blame]
bulach@chromium.orga49afdc2013-06-13 00:33:47 +09001// 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
skuhne4bf947f2014-12-17 09:01:49 +09006// platforms that have such signals, such as Android and ChromeOS.
bulach@chromium.orga49afdc2013-06-13 00:33:47 +09007// The app will try to discard buffers that aren't deemed essential (individual
8// modules will implement their own policy).
bulach@chromium.orga49afdc2013-06-13 00:33:47 +09009
danakj0393da32015-03-10 09:31:16 +090010#ifndef BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_
11#define BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090012
13#include "base/base_export.h"
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090014#include "base/callback.h"
avi67511492015-12-24 17:44:47 +090015#include "base/macros.h"
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090016
17namespace 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.orgfe8a13b2013-10-03 02:57:09 +090024// Note that even on the same thread, the callback is not guaranteed to be
25// called synchronously within the system memory pressure broadcast.
skuhne4bf947f2014-12-17 09:01:49 +090026// 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.orga49afdc2013-06-13 00:33:47 +090029// cold-started.
30//
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090031// 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//
46class BASE_EXPORT MemoryPressureListener {
47 public:
mkosibad1fb9d42014-10-23 22:56:12 +090048 // A Java counterpart will be generated for this enum.
49 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090050 enum MemoryPressureLevel {
skuhne4bf947f2014-12-17 09:01:49 +090051 // 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.zheng5996d392016-05-11 11:33:39 +090054 MEMORY_PRESSURE_LEVEL_NONE,
skuhne4bf947f2014-12-17 09:01:49 +090055
mkosibad1fb9d42014-10-23 22:56:12 +090056 // Modules are advised to free buffers that are cheap to re-allocate and not
57 // immediately needed.
hong.zheng5996d392016-05-11 11:33:39 +090058 MEMORY_PRESSURE_LEVEL_MODERATE,
mkosibad1fb9d42014-10-23 22:56:12 +090059
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.zheng5996d392016-05-11 11:33:39 +090063 MEMORY_PRESSURE_LEVEL_CRITICAL,
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090064 };
65
hong.zheng5996d392016-05-11 11:33:39 +090066 typedef Callback<void(MemoryPressureLevel)> MemoryPressureCallback;
67 typedef Callback<void(MemoryPressureLevel)> SyncMemoryPressureCallback;
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090068
69 explicit MemoryPressureListener(
70 const MemoryPressureCallback& memory_pressure_callback);
hong.zheng5996d392016-05-11 11:33:39 +090071 MemoryPressureListener(
72 const MemoryPressureCallback& memory_pressure_callback,
73 const SyncMemoryPressureCallback& sync_memory_pressure_callback);
74
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090075 ~MemoryPressureListener();
76
77 // Intended for use by the platform specific implementation.
78 static void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level);
79
petrcermak595fa8f2015-09-07 20:25:31 +090080 // 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);
petrcermakb7fd0b02015-09-28 22:05:18 +090085 static void SimulatePressureNotification(
86 MemoryPressureLevel memory_pressure_level);
petrcermak595fa8f2015-09-07 20:25:31 +090087
vitalybuka3e6a4a82016-05-05 05:50:48 +090088 void Notify(MemoryPressureLevel memory_pressure_level);
hong.zheng5996d392016-05-11 11:33:39 +090089 void SyncNotify(MemoryPressureLevel memory_pressure_level);
vitalybuka3e6a4a82016-05-05 05:50:48 +090090
hong.zheng5996d392016-05-11 11:33:39 +090091 private:
petrcermakb7fd0b02015-09-28 22:05:18 +090092 static void DoNotifyMemoryPressure(MemoryPressureLevel memory_pressure_level);
93
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090094 MemoryPressureCallback callback_;
hong.zheng5996d392016-05-11 11:33:39 +090095 SyncMemoryPressureCallback sync_memory_pressure_callback_;
bulach@chromium.orga49afdc2013-06-13 00:33:47 +090096
97 DISALLOW_COPY_AND_ASSIGN(MemoryPressureListener);
98};
99
100} // namespace base
101
danakj0393da32015-03-10 09:31:16 +0900102#endif // BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_