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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 52 |
54 namespace base { | 53 namespace base { |
55 namespace internal { | 54 namespace internal { |
56 | 55 |
57 // The DiscardableMemoryManager manages a collection of | 56 // The DiscardableMemoryManager manages a collection of |
58 // DiscardableMemoryManagerAllocation instances. It is used on platforms that | 57 // DiscardableMemoryManagerAllocation instances. It is used on platforms that |
59 // need some level of userspace control over discardable memory. It keeps track | 58 // need some level of userspace control over discardable memory. It keeps track |
60 // of all allocation instances (in case they need to be purged), and the total | 59 // of all allocation instances (in case they need to be purged), and the total |
61 // amount of allocated memory (in case this forces a purge). When memory usage | 60 // amount of allocated memory (in case this forces a purge). When memory usage |
62 // reaches the limit, the manager purges the LRU memory. | 61 // reaches the limit, the manager purges the LRU memory. |
63 // | |
64 // When notified of memory pressure, the manager either purges the LRU memory -- | |
65 // if the pressure is moderate -- or all discardable memory if the pressure is | |
66 // critical. | |
67 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { | 62 class BASE_EXPORT_PRIVATE DiscardableMemoryManager { |
68 public: | 63 public: |
69 typedef DiscardableMemoryManagerAllocation Allocation; | 64 typedef DiscardableMemoryManagerAllocation Allocation; |
70 | 65 |
71 DiscardableMemoryManager(size_t memory_limit, | 66 DiscardableMemoryManager(size_t memory_limit, |
72 size_t soft_memory_limit, | 67 size_t soft_memory_limit, |
73 size_t bytes_to_keep_under_moderate_pressure, | |
74 TimeDelta hard_memory_limit_expiration_time); | 68 TimeDelta hard_memory_limit_expiration_time); |
75 virtual ~DiscardableMemoryManager(); | 69 virtual ~DiscardableMemoryManager(); |
76 | 70 |
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 | 71 // The maximum number of bytes of memory that may be allocated before we force |
85 // a purge. | 72 // a purge. |
86 void SetMemoryLimit(size_t bytes); | 73 void SetMemoryLimit(size_t bytes); |
87 | 74 |
88 // The number of bytes of memory that may be allocated but unused for the hard | 75 // The number of bytes of memory that may be allocated but unused for the hard |
89 // limit expiration time without getting purged. | 76 // limit expiration time without getting purged. |
90 void SetSoftMemoryLimit(size_t bytes); | 77 void SetSoftMemoryLimit(size_t bytes); |
91 | 78 |
92 // Sets the amount of memory to keep when we're under moderate pressure. | |
93 void SetBytesToKeepUnderModeratePressure(size_t bytes); | |
94 | |
95 // Sets the memory usage cutoff time for hard memory limit. | 79 // Sets the memory usage cutoff time for hard memory limit. |
96 void SetHardMemoryLimitExpirationTime( | 80 void SetHardMemoryLimitExpirationTime( |
97 TimeDelta hard_memory_limit_expiration_time); | 81 TimeDelta hard_memory_limit_expiration_time); |
98 | 82 |
99 // This will attempt to reduce memory footprint until within soft memory | 83 // 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 | 84 // limit. Returns true if there's no need to call this again until allocations |
101 // have been used. | 85 // have been used. |
102 bool ReduceMemoryUsage(); | 86 bool ReduceMemoryUsage(); |
103 | 87 |
| 88 // This can be called to attempt to reduce memory footprint until within |
| 89 // limit for bytes to keep under moderate pressure. |
| 90 void ReduceMemoryUsageUntilWithinLimit(size_t bytes); |
| 91 |
104 // Adds the given allocation to the manager's collection. | 92 // Adds the given allocation to the manager's collection. |
105 void Register(Allocation* allocation, size_t bytes); | 93 void Register(Allocation* allocation, size_t bytes); |
106 | 94 |
107 // Removes the given allocation from the manager's collection. | 95 // Removes the given allocation from the manager's collection. |
108 void Unregister(Allocation* allocation); | 96 void Unregister(Allocation* allocation); |
109 | 97 |
110 // Returns false if an error occurred. Otherwise, returns true and sets | 98 // Returns false if an error occurred. Otherwise, returns true and sets |
111 // |purged| to indicate whether or not allocation has been purged since last | 99 // |purged| to indicate whether or not allocation has been purged since last |
112 // use. | 100 // use. |
113 bool AcquireLock(Allocation* allocation, bool* purged); | 101 bool AcquireLock(Allocation* allocation, bool* purged); |
(...skipping 20 matching lines...) Expand all Loading... |
134 private: | 122 private: |
135 struct AllocationInfo { | 123 struct AllocationInfo { |
136 explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} | 124 explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {} |
137 | 125 |
138 const size_t bytes; | 126 const size_t bytes; |
139 bool purgable; | 127 bool purgable; |
140 TimeTicks last_usage; | 128 TimeTicks last_usage; |
141 }; | 129 }; |
142 typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; | 130 typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap; |
143 | 131 |
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 | |
149 // |bytes_to_keep_under_moderate_pressure_|. | |
150 void PurgeUntilWithinBytesToKeepUnderModeratePressure(); | |
151 | |
152 // Purges memory not used since |hard_memory_limit_expiration_time_| before | 132 // Purges memory not used since |hard_memory_limit_expiration_time_| before |
153 // "right now" until usage is less or equal to |soft_memory_limit_|. | 133 // "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 | 134 // Returns true if total amount of memory is less or equal to soft memory |
155 // limit. | 135 // limit. |
156 bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); | 136 bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); |
157 | 137 |
158 // Purges memory that has not been used since |timestamp| until usage is less | 138 // Purges memory that has not been used since |timestamp| until usage is less |
159 // or equal to |limit|. | 139 // or equal to |limit|. |
160 // Caller must acquire |lock_| prior to calling this function. | 140 // Caller must acquire |lock_| prior to calling this function. |
161 void PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired( | 141 void PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired( |
(...skipping 16 matching lines...) Expand all Loading... |
178 size_t bytes_allocated_; | 158 size_t bytes_allocated_; |
179 | 159 |
180 // The maximum number of bytes of memory that may be allocated. | 160 // The maximum number of bytes of memory that may be allocated. |
181 size_t memory_limit_; | 161 size_t memory_limit_; |
182 | 162 |
183 // The number of bytes of memory that may be allocated but not used for | 163 // 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 | 164 // |hard_memory_limit_expiration_time_| amount of time when receiving an idle |
185 // notification. | 165 // notification. |
186 size_t soft_memory_limit_; | 166 size_t soft_memory_limit_; |
187 | 167 |
188 // Under moderate memory pressure, we will purge memory until usage is within | |
189 // this limit. | |
190 size_t bytes_to_keep_under_moderate_pressure_; | |
191 | |
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 | 168 // Amount of time it takes for an allocation to become affected by |
197 // |soft_memory_limit_|. | 169 // |soft_memory_limit_|. |
198 TimeDelta hard_memory_limit_expiration_time_; | 170 TimeDelta hard_memory_limit_expiration_time_; |
199 | 171 |
200 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); | 172 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager); |
201 }; | 173 }; |
202 | 174 |
203 } // namespace internal | 175 } // namespace internal |
204 } // namespace base | 176 } // namespace base |
205 | 177 |
206 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ | 178 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_ |
OLD | NEW |