-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Runtime] Support clear global memory allocators #16066
[Runtime] Support clear global memory allocators #16066
Conversation
src/runtime/memory/memory_manager.cc
Outdated
void MemoryManager::Clear() { | ||
MemoryManager* m = MemoryManager::Global(); | ||
std::lock_guard<std::mutex> lock(m->mu_); | ||
m->allocators_.clear(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let us iterate over each allocator and clear each of the freed entries, as oppose to clear the vector, which can cause issues when some of the allocated storage from the allocators get retained and deleted after clear
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing it out. Just pushed a new commit, not sure if it addresses in the exactly same way as you mentioned.
Any updates? The latest rebase/merge will depend on this PR |
@@ -166,6 +167,18 @@ Allocator* MemoryManager::GetAllocator(Device dev, AllocatorType type) { | |||
return it->second.at(type).get(); | |||
} | |||
|
|||
void MemoryManager::Clear() { | |||
MemoryManager* m = MemoryManager::Global(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to add a Clear interface to base Allocator, and iterate over the allocators, to call Allocator->Clear() without removing any allocators
7d0c218
to
bcb2262
Compare
This PR supports clearing up all the allocated memory. Prior to this PR, all the allocated memory are managed in the pool of memory manager. The allocated memory in the pool is on hold and never freed. Consequently, the pool size always goes up monotonically within a single run in a process. While good to save time of memory allocation, in some cases (e.g., on mobile phones which may have running memory limit) we need to clear the pool and free all the memory in order to prevent the pool from endlessly growing up and some of allocated memory not being effectively utilized (fragmentation). Therefore, this PR introduces a PackedFunc that helps clean up the memory manager, releasing all the allocated memory. Runtime apps can decide when to invoke this PackedFunc and clean up the pool. Usually, this will happen at some app "reset" or "reload" stage.
bcb2262
to
21e77ce
Compare
This PR supports clearing up all the allocated memory.
Prior to this PR, all the allocated memory are managed in the pool of memory manager. The allocated memory in the pool is on hold and never freed. Consequently, the pool size always goes up monotonically within a single run in a process.
While good to save time of memory allocation, in some cases (e.g., on mobile phones which may have running memory limit) we need to clear the pool and free all the memory in order to prevent the pool from endlessly growing up and some of allocated memory not being effectively utilized (fragmentation).
Therefore, this PR introduces a PackedFunc that helps clean up the memory manager, releasing all the allocated memory. Runtime apps can decide when to invoke this PackedFunc and clean up the pool. Usually, this will happen at some app "reset" or "reload" stage.