Skip to content
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

Merged

Conversation

MasterJH5574
Copy link
Contributor

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.

void MemoryManager::Clear() {
MemoryManager* m = MemoryManager::Global();
std::lock_guard<std::mutex> lock(m->mu_);
m->allocators_.clear();
Copy link
Member

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

Copy link
Contributor Author

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.

@junrushao
Copy link
Member

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();
Copy link
Member

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

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.
@MasterJH5574 MasterJH5574 force-pushed the tvm-dev/2023-11-03-memory-manager-clear branch from bcb2262 to 21e77ce Compare November 5, 2023 22:50
@junrushao junrushao merged commit 02d4df7 into apache:main Nov 6, 2023
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants