-
Notifications
You must be signed in to change notification settings - Fork 696
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
How to free memory allocated by SEAL? #241
Comments
When you create new parameter sets, the buffer allocation sizes change totally. SEAL memory pool only reuses memory allocations if their sizes match exactly. The justification for that is that in many cases we would expect an application reuse the same set of parameters multiple times, and with exact size match we can ensure there is no "wasted space". However, now you run into this problem. All allocations that you are making are taken from the so-called global memory pool. For example, the constructor of Since your code doesn't necessarily make sense as a real application, it's a little hard to say how exactly you should do this, but one way is as follows. Change the beginning of your loop to: for(int i = 1; i < 7; i++) {
MemoryPoolHandle my_pool = MemoryPoolHandle::New();
MemoryManager::SwitchProfile(make_unique<MMProfFixed>(std::move(my_pool)));
EncryptionParameters parms(scheme_type::CKKS);
// Your code here ... Here you first create a new memory pool. Then you switch the memory manager profile to a "fixed" profile that always returns the same pool whenever Once the loop completes, however, you may want to switch back to the previous profile (the one that always returns the global memory pool). This can be done as follows: for(int i = 1; i < 7; i++) {
MemoryPoolHandle my_pool = MemoryPoolHandle::New();
auto old_prof = MemoryManager::SwitchProfile(make_unique<MMProfFixed>(std::move(my_pool)));
EncryptionParameters parms(scheme_type::CKKS);
// Your code here ...
auto gal_keys = keygen.galois_keys_local();
MemoryManager::SwitchProfile(std::move(old_prof));
} You can also use the for(int i = 1; i < 7; i++) {
MemoryPoolHandle my_pool = MemoryPoolHandle::New();
auto pg = MemoryManager::MMProfGuard(make_unique<MMProfFixed>(std::move(my_pool)));
EncryptionParameters parms(scheme_type::CKKS);
// Your code here ...
auto gal_keys = keygen.galois_keys_local();
} If you need some custom memory pool behavior, it's easy to create your own memory manager profile (just inherit from |
Kim, thanks for your detailed answer. However, I'd appreciate your help understanding what's happening with the 1st and 3rd snippets in your answer. The first snippet is the same as the second, except that you don't reset to use the global memory pool at the end of the loop. When I do that, I get the an odd memory usage profile that is different from the original, but which still leaks memory over time. Specifically:
Memory usage at beginning and end of each loop iteration:
So you can see that some memory is being freed at the end of each loop (when my_pool is released), but some of the memory is retained. For your 3rd snippet, I'm assuming you meant to write |
Sorry that this issue has been here for such a long time. I've tried to reproduce this issue with version 3.7.1. It seems like that it has been resolved overtime. At least from my side I don't see a memory leakage. Do you still have this issue? |
For testing and benchmarking, I wrote a loop which allocates several different CKKS parameters and generates keys for those parameters. I expected that all memory allocated for the keys/params is freed at the end of the
for
loop. Instead, this memory is apparently not freed.I tracked memory usage at the start of each
for
iteration:Start of i=1: 905M
Start of i=2: 1.2G
Start of i=3: 2.1G
Start of 1=4: 3.7G
Start of i=5: 6.4G
Start of i=6: 10G
How can I free memory allocated by SEAL?
The text was updated successfully, but these errors were encountered: