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. Clients are expected | |
22 // to update their allocation policies (e.g. setting cache limit) that persist | |
23 // for the duration of the memory state. Purging requests will be throttled as | |
24 // well. | |
chrisha
2017/01/26 22:17:35
A comment that OnMemoryStateChange should only aff
bashi
2017/01/26 23:44:59
Done.
| |
21 | 25 |
22 // MemoryState is an indicator that processes can use to guide their memory | 26 // MemoryState is an indicator that processes can use to guide their memory |
23 // allocation policies. For example, a process that receives the suspended | 27 // allocation policies. For example, a process that receives the throttled |
24 // state can use that as as signal to drop memory caches. | 28 // 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 | 29 // NOTE: This enum is used to back an UMA histogram, and therefore should be |
26 // treated as append-only. | 30 // treated as append-only. |
27 enum class MemoryState : int { | 31 enum class MemoryState : int { |
28 // The state is unknown. | 32 // The state is unknown. |
29 UNKNOWN = -1, | 33 UNKNOWN = -1, |
30 // No memory constraints. | 34 // No memory constraints. |
31 NORMAL = 0, | 35 NORMAL = 0, |
32 // Running and interactive but allocation should be throttled. | 36 // Running and interactive but memory allocation should be throttled. |
33 // Clients should free up any memory that is used as an optimization but | 37 // 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). | 38 // optimization but that is not necessary for the process to run. |
39 // (e.g. caches) | |
35 THROTTLED = 1, | 40 THROTTLED = 1, |
36 // Still resident in memory but core processing logic has been suspended. | 41 // 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 | 42 // 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, | 43 SUSPENDED = 2, |
41 }; | 44 }; |
42 | 45 |
43 const int kMemoryStateMax = static_cast<int>(MemoryState::SUSPENDED) + 1; | 46 const int kMemoryStateMax = static_cast<int>(MemoryState::SUSPENDED) + 1; |
44 | 47 |
45 // Returns a string representation of MemoryState. | 48 // Returns a string representation of MemoryState. |
46 BASE_EXPORT const char* MemoryStateToString(MemoryState state); | 49 BASE_EXPORT const char* MemoryStateToString(MemoryState state); |
47 | 50 |
48 // This is an interface for components which can respond to memory status | 51 // This is an interface for components which can respond to memory status |
49 // changes. An initial state is NORMAL. See MemoryCoordinatorClientRegistry for | 52 // changes. An initial state is NORMAL. See MemoryCoordinatorClientRegistry for |
50 // threading guarantees and ownership management. | 53 // threading guarantees and ownership management. |
51 class BASE_EXPORT MemoryCoordinatorClient { | 54 class BASE_EXPORT MemoryCoordinatorClient { |
52 public: | 55 public: |
53 // Called when memory state has changed. Any transition can occur except for | 56 // Called when memory state has changed. Any transition can occur except for |
54 // UNKNOWN. General guidelines are: | 57 // UNKNOWN. General guidelines are: |
55 // * NORMAL: Restore the default settings for memory allocation/usage if | 58 // * NORMAL: Restore the default settings for memory allocation/usage if |
56 // it has changed. | 59 // it has changed. |
57 // * THROTTLED: Use smaller limits for memory allocations and caches. | 60 // * THROTTLED: Use smaller limits for future memory allocations. You don't |
58 // * SUSPENDED: Purge memory. | 61 // need to take any action on existing allocations. |
59 virtual void OnMemoryStateChange(MemoryState state) = 0; | 62 // * SUSPENDED: Use much smaller limits for future memory allocations. |
63 virtual void OnMemoryStateChange(MemoryState state) {} | |
60 | 64 |
61 protected: | 65 // Called to purge memory. |
66 // This callback should free up any memory that is used as an optimization, or | |
67 // any memory whose contents can be reproduced. | |
68 virtual void OnPurgeMemory() {} | |
69 | |
70 protected: | |
62 virtual ~MemoryCoordinatorClient() {} | 71 virtual ~MemoryCoordinatorClient() {} |
63 }; | 72 }; |
64 | 73 |
65 } // namespace base | 74 } // namespace base |
66 | 75 |
67 #endif // BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_ | 76 #endif // BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_ |
OLD | NEW |