greg7mdp / parallel-hashmap

A family of header-only, very fast and memory-friendly hashmap and btree containers.
https://greg7mdp.github.io/parallel-hashmap/
Apache License 2.0
2.53k stars 239 forks source link

Release memory after shrinking #236

Closed benstadin closed 7 months ago

benstadin commented 7 months ago

As far as I understand Google's sparse hash map maintains a low memory footprint by deallocation memory after a certain threshold. Does parallel-hashmap provide a similar feature in order to release memory when the map size is reduced?

greg7mdp commented 7 months ago

No, it doesn't. What is your exact use case? If you want to use as little memory as possible, I recommend using sparsepp.

benstadin commented 7 months ago

I have multiple maps which independently grow and shrink in size during the lifetime of the application. This application runs multiple days and thus the overall memory required by all maps is very high.

greg7mdp commented 7 months ago

You really should try sparsepp, it should work well for this. It has not been updated in a while though, so missing newer C++ APIs, but still more modern and up-to-date than Google's sparsehash map, which it is derived from.

benstadin commented 7 months ago

Does it actively free memory when resizing?

greg7mdp commented 7 months ago

To guarantee that a map's memory use is cleared, you can swap it with a temporary map, for example:

MyMap map;
map.emplace(....);
...
MyMap().swap(map); // swap map with an empty temporary, which is immediately destroyed
greg7mdp commented 7 months ago

Does it actively free memory when resizing?

@benstadin yes it does.