OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_ | 5 #ifndef BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_ |
6 #define BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_ | 6 #define BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 | 11 |
12 // OVERVIEW: | 12 // OVERVIEW: |
13 // | 13 // |
14 // MemoryCoordinatorClient is an interface which a component can implement to | 14 // MemoryCoordinatorClient is an interface which a component can implement to |
15 // respond to memory state changes. Unlike MemoryPressureListener, this is a | 15 // adjust "future allocation" and "existing allocation". For "future allocation" |
16 // stateful mechanism and clients receive notifications only when memory states | 16 // it provides a callback to observe memory state changes, and for "existing |
17 // are changed. State transitions are throttled to avoid thrashing; the exact | 17 // allocation" it provides a callback to purge memory. |
18 // throttling period is platform dependent, but will be at least 5-10 seconds. | 18 // |
19 // Clients are expected to make changes in memory usage that persist for the | 19 // Unlike MemoryPressureListener, memory state changes are stateful. State |
20 // duration of the memory state. | 20 // transitions are throttled to avoid thrashing; the exact throttling period is |
| 21 // platform dependent, but will be at least 5-10 seconds. When a state change |
| 22 // notification is dispatched, clients are expected to update their allocation |
| 23 // policies (e.g. setting cache limit) that persist for the duration of the |
| 24 // memory state. Note that clients aren't expected to free up memory on memory |
| 25 // state changes. Clients should wait for a separate purge request to free up |
| 26 // memory. Purging requests will be throttled as well. |
21 | 27 |
22 // MemoryState is an indicator that processes can use to guide their memory | 28 // MemoryState is an indicator that processes can use to guide their memory |
23 // allocation policies. For example, a process that receives the suspended | 29 // allocation policies. For example, a process that receives the throttled |
24 // state can use that as as signal to drop memory caches. | 30 // state can use that as as signal to decrease memory cache limits. |
25 // NOTE: This enum is used to back an UMA histogram, and therefore should be | 31 // NOTE: This enum is used to back an UMA histogram, and therefore should be |
26 // treated as append-only. | 32 // treated as append-only. |
27 enum class MemoryState : int { | 33 enum class MemoryState : int { |
28 // The state is unknown. | 34 // The state is unknown. |
29 UNKNOWN = -1, | 35 UNKNOWN = -1, |
30 // No memory constraints. | 36 // No memory constraints. |
31 NORMAL = 0, | 37 NORMAL = 0, |
32 // Running and interactive but allocation should be throttled. | 38 // Running and interactive but memory allocation should be throttled. |
33 // Clients should free up any memory that is used as an optimization but | 39 // Clients should set lower budget for any memory that is used as an |
34 // that is not necessary for the process to run (e.g. caches). | 40 // optimization but that is not necessary for the process to run. |
| 41 // (e.g. caches) |
35 THROTTLED = 1, | 42 THROTTLED = 1, |
36 // Still resident in memory but core processing logic has been suspended. | 43 // Still resident in memory but core processing logic has been suspended. |
37 // Clients should free up any memory that is used as an optimization, or | 44 // In most cases, OnPurgeMemory() will be called before entering this state. |
38 // any memory whose contents can be reproduced when transitioning out of | |
39 // the suspended state (e.g. parsed resource that can be reloaded from disk). | |
40 SUSPENDED = 2, | 45 SUSPENDED = 2, |
41 }; | 46 }; |
42 | 47 |
43 const int kMemoryStateMax = static_cast<int>(MemoryState::SUSPENDED) + 1; | 48 const int kMemoryStateMax = static_cast<int>(MemoryState::SUSPENDED) + 1; |
44 | 49 |
45 // Returns a string representation of MemoryState. | 50 // Returns a string representation of MemoryState. |
46 BASE_EXPORT const char* MemoryStateToString(MemoryState state); | 51 BASE_EXPORT const char* MemoryStateToString(MemoryState state); |
47 | 52 |
48 // This is an interface for components which can respond to memory status | 53 // This is an interface for components which can respond to memory status |
49 // changes. An initial state is NORMAL. See MemoryCoordinatorClientRegistry for | 54 // changes. An initial state is NORMAL. See MemoryCoordinatorClientRegistry for |
50 // threading guarantees and ownership management. | 55 // threading guarantees and ownership management. |
51 class BASE_EXPORT MemoryCoordinatorClient { | 56 class BASE_EXPORT MemoryCoordinatorClient { |
52 public: | 57 public: |
53 // Called when memory state has changed. Any transition can occur except for | 58 // Called when memory state has changed. Any transition can occur except for |
54 // UNKNOWN. General guidelines are: | 59 // UNKNOWN. General guidelines are: |
55 // * NORMAL: Restore the default settings for memory allocation/usage if | 60 // * NORMAL: Restore the default settings for memory allocation/usage if |
56 // it has changed. | 61 // it has changed. |
57 // * THROTTLED: Use smaller limits for memory allocations and caches. | 62 // * THROTTLED: Use smaller limits for future memory allocations. You don't |
58 // * SUSPENDED: Purge memory. | 63 // need to take any action on existing allocations. |
59 virtual void OnMemoryStateChange(MemoryState state) = 0; | 64 // * SUSPENDED: Use much smaller limits for future memory allocations. You |
| 65 // don't need to take any action on existing allocations. |
| 66 virtual void OnMemoryStateChange(MemoryState state) {} |
60 | 67 |
61 protected: | 68 // Called to purge memory. |
| 69 // This callback should free up any memory that is used as an optimization, or |
| 70 // any memory whose contents can be reproduced. |
| 71 virtual void OnPurgeMemory() {} |
| 72 |
| 73 protected: |
62 virtual ~MemoryCoordinatorClient() {} | 74 virtual ~MemoryCoordinatorClient() {} |
63 }; | 75 }; |
64 | 76 |
65 } // namespace base | 77 } // namespace base |
66 | 78 |
67 #endif // BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_ | 79 #endif // BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_ |
OLD | NEW |