memory_reclaimer.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2019 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. #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_MEMORY_RECLAIMER_H_
  5. #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_MEMORY_RECLAIMER_H_
  6. #include <memory>
  7. #include <set>
  8. #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
  9. #include "base/allocator/partition_allocator/partition_alloc_base/no_destructor.h"
  10. #include "base/allocator/partition_allocator/partition_alloc_base/thread_annotations.h"
  11. #include "base/allocator/partition_allocator/partition_alloc_base/time/time.h"
  12. #include "base/allocator/partition_allocator/partition_alloc_forward.h"
  13. #include "base/allocator/partition_allocator/partition_lock.h"
  14. namespace partition_alloc {
  15. // Posts and handles memory reclaim tasks for PartitionAlloc.
  16. //
  17. // Thread safety: |RegisterPartition()| and |UnregisterPartition()| can be
  18. // called from any thread, concurrently with reclaim. Reclaim itself runs in the
  19. // context of the provided |SequencedTaskRunner|, meaning that the caller must
  20. // take care of this runner being compatible with the various partitions.
  21. //
  22. // Singleton as this runs as long as the process is alive, and
  23. // having multiple instances would be wasteful.
  24. class PA_COMPONENT_EXPORT(PARTITION_ALLOC) MemoryReclaimer {
  25. public:
  26. static MemoryReclaimer* Instance();
  27. MemoryReclaimer(const MemoryReclaimer&) = delete;
  28. MemoryReclaimer& operator=(const MemoryReclaimer&) = delete;
  29. // Internal. Do not use.
  30. // Registers a partition to be tracked by the reclaimer.
  31. void RegisterPartition(PartitionRoot<>* partition);
  32. // Internal. Do not use.
  33. // Unregisters a partition to be tracked by the reclaimer.
  34. void UnregisterPartition(PartitionRoot<>* partition);
  35. // Triggers an explicit reclaim now to reclaim as much free memory as
  36. // possible. The API callers need to invoke this method periodically
  37. // if they want to use memory reclaimer.
  38. // See also GetRecommendedReclaimIntervalInMicroseconds()'s comment.
  39. void ReclaimNormal();
  40. // Returns a recommended interval to invoke ReclaimNormal.
  41. int64_t GetRecommendedReclaimIntervalInMicroseconds() {
  42. return internal::base::Seconds(4).InMicroseconds();
  43. }
  44. // Triggers an explicit reclaim now reclaiming all free memory
  45. void ReclaimAll();
  46. private:
  47. MemoryReclaimer();
  48. ~MemoryReclaimer();
  49. // |flags| is an OR of base::PartitionPurgeFlags
  50. void Reclaim(int flags);
  51. void ReclaimAndReschedule();
  52. void ResetForTesting();
  53. internal::Lock lock_;
  54. std::set<PartitionRoot<>*> partitions_ PA_GUARDED_BY(lock_);
  55. friend class internal::base::NoDestructor<MemoryReclaimer>;
  56. friend class MemoryReclaimerTest;
  57. };
  58. } // namespace partition_alloc
  59. #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_MEMORY_RECLAIMER_H_