OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_DISCARDABLE_MEMORY_MANAGER_H_ | 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/containers/mru_cache.h" | 10 #include "base/containers/mru_cache.h" |
11 #include "base/memory/memory_pressure_listener.h" | |
12 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
14 | 13 |
15 namespace base { | 14 namespace base { |
16 namespace internal { | 15 namespace internal { |
17 | 16 |
18 // This interface is used by the DiscardableMemoryManager class to provide some | 17 // This interface is used by the DiscardableMemoryManager class to provide some |
19 // level of userspace control over discardable memory allocations. | 18 // level of userspace control over discardable memory allocations. |
20 class DiscardableMemoryManagerAllocation { | 19 class DiscardableMemoryManagerAllocation { |
21 public: | 20 public: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { | 66 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { |
68 public: | 67 public: |
69 typedef DiscardableMemoryManagerAllocation Allocation; | 68 typedef DiscardableMemoryManagerAllocation Allocation; |
70 | 69 |
71 DiscardableMemoryManager(size_t memory_limit, | 70 DiscardableMemoryManager(size_t memory_limit, |
72 size_t soft_memory_limit, | 71 size_t soft_memory_limit, |
73 size_t bytes_to_keep_under_moderate_pressure, | 72 size_t bytes_to_keep_under_moderate_pressure, |
74 TimeDelta hard_memory_limit_expiration_time); | 73 TimeDelta hard_memory_limit_expiration_time); |
75 virtual ~DiscardableMemoryManager(); | 74 virtual ~DiscardableMemoryManager(); |
76 | 75 |
77 // Call this to register memory pressure listener. Must be called on a thread | |
78 // with a MessageLoop current. | |
79 void RegisterMemoryPressureListener(); | |
80 | |
81 // Call this to unregister memory pressure listener. | |
82 void UnregisterMemoryPressureListener(); | |
83 | |
84 // The maximum number of bytes of memory that may be allocated before we force | 76 // The maximum number of bytes of memory that may be allocated before we force |
85 // a purge. | 77 // a purge. |
86 void SetMemoryLimit(size_t bytes); | 78 void SetMemoryLimit(size_t bytes); |
87 | 79 |
88 // The number of bytes of memory that may be allocated but unused for the hard | 80 // The number of bytes of memory that may be allocated but unused for the hard |
89 // limit expiration time without getting purged. | 81 // limit expiration time without getting purged. |
90 void SetSoftMemoryLimit(size_t bytes); | 82 void SetSoftMemoryLimit(size_t bytes); |
91 | 83 |
92 // Sets the amount of memory to keep when we're under moderate pressure. | 84 // Sets the amount of memory to keep when we're under moderate pressure. |
93 void SetBytesToKeepUnderModeratePressure(size_t bytes); | 85 void SetBytesToKeepUnderModeratePressure(size_t bytes); |
94 | 86 |
95 // Sets the memory usage cutoff time for hard memory limit. | 87 // Sets the memory usage cutoff time for hard memory limit. |
96 void SetHardMemoryLimitExpirationTime( | 88 void SetHardMemoryLimitExpirationTime( |
97 TimeDelta hard_memory_limit_expiration_time); | 89 TimeDelta hard_memory_limit_expiration_time); |
98 | 90 |
99 // This will attempt to reduce memory footprint until within soft memory | 91 // This will attempt to reduce memory footprint until within soft memory |
100 // limit. Returns true if there's no need to call this again until allocations | 92 // limit. Returns true if there's no need to call this again until allocations |
101 // have been used. | 93 // have been used. |
102 bool ReduceMemoryUsage(); | 94 bool ReduceMemoryUsage(); |
103 | 95 |
| 96 // This can be called to attempt to reduce memory footprint until within |
| 97 // limit for bytes to keep under moderate pressure. |
| 98 void ReduceMemoryUsageUntilWithinModeratePressureLimit(); |
| 99 |
104 // Adds the given allocation to the manager's collection. | 100 // Adds the given allocation to the manager's collection. |
105 void Register(Allocation* allocation, size_t bytes); | 101 void Register(Allocation* allocation, size_t bytes); |
106 | 102 |
107 // Removes the given allocation from the manager's collection. | 103 // Removes the given allocation from the manager's collection. |
108 void Unregister(Allocation* allocation); | 104 void Unregister(Allocation* allocation); |
109 | 105 |
110 // Returns false if an error occurred. Otherwise, returns true and sets | 106 // Returns false if an error occurred. Otherwise, returns true and sets |
111 // |purged| to indicate whether or not allocation has been purged since last | 107 // |purged| to indicate whether or not allocation has been purged since last |
112 // use. | 108 // use. |
113 bool AcquireLock(Allocation* allocation, bool* purged); | 109 bool AcquireLock(Allocation* allocation, bool* purged); |
(...skipping 20 matching lines...) Expand all Loading... |
134 private: | 130 private: |
135 struct AllocationInfo { | 131 struct AllocationInfo { |
136 explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} | 132 explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} |
137 | 133 |
138 const size_t bytes; | 134 const size_t bytes; |
139 bool purgable; | 135 bool purgable; |
140 TimeTicks last_usage; | 136 TimeTicks last_usage; |
141 }; | 137 }; |
142 typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; | 138 typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; |
143 | 139 |
144 // This can be called as a hint that the system is under memory pressure. | |
145 void OnMemoryPressure( | |
146 MemoryPressureListener::MemoryPressureLevel pressure_level); | |
147 | |
148 // Purges memory until usage is less or equal to | 140 // Purges memory until usage is less or equal to |
149 // |bytes_to_keep_under_moderate_pressure_|. | 141 // |bytes_to_keep_under_moderate_pressure_|. |
150 void PurgeUntilWithinBytesToKeepUnderModeratePressure(); | 142 void PurgeUntilWithinBytesToKeepUnderModeratePressure(); |
151 | 143 |
152 // Purges memory not used since |hard_memory_limit_expiration_time_| before | 144 // Purges memory not used since |hard_memory_limit_expiration_time_| before |
153 // "right now" until usage is less or equal to |soft_memory_limit_|. | 145 // "right now" until usage is less or equal to |soft_memory_limit_|. |
154 // Returns true if total amount of memory is less or equal to soft memory | 146 // Returns true if total amount of memory is less or equal to soft memory |
155 // limit. | 147 // limit. |
156 bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); | 148 bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); |
157 | 149 |
(...skipping 24 matching lines...) Expand all Loading... |
182 | 174 |
183 // The number of bytes of memory that may be allocated but not used for | 175 // The number of bytes of memory that may be allocated but not used for |
184 // |hard_memory_limit_expiration_time_| amount of time when receiving an idle | 176 // |hard_memory_limit_expiration_time_| amount of time when receiving an idle |
185 // notification. | 177 // notification. |
186 size_t soft_memory_limit_; | 178 size_t soft_memory_limit_; |
187 | 179 |
188 // Under moderate memory pressure, we will purge memory until usage is within | 180 // Under moderate memory pressure, we will purge memory until usage is within |
189 // this limit. | 181 // this limit. |
190 size_t bytes_to_keep_under_moderate_pressure_; | 182 size_t bytes_to_keep_under_moderate_pressure_; |
191 | 183 |
192 // Allows us to be respond when the system reports that it is under memory | |
193 // pressure. | |
194 scoped_ptr<MemoryPressureListener> memory_pressure_listener_; | |
195 | |
196 // Amount of time it takes for an allocation to become affected by | 184 // Amount of time it takes for an allocation to become affected by |
197 // |soft_memory_limit_|. | 185 // |soft_memory_limit_|. |
198 TimeDelta hard_memory_limit_expiration_time_; | 186 TimeDelta hard_memory_limit_expiration_time_; |
199 | 187 |
200 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); | 188 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); |
201 }; | 189 }; |
202 | 190 |
203 } // namespace internal | 191 } // namespace internal |
204 } // namespace base | 192 } // namespace base |
205 | 193 |
206 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 194 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |
OLD | NEW |