Memory management:when rust return memory to OS?

I have done a test: use hashmap insert some string, and use cmd"top -pid" to show the progressInfo,the memory footprint slowly gets higher, but when I remove,the memory footprint never change even the hashmap have none data。
so,why memory footprint unchanged?when rust return memory to OS?
pls help me!
thanks!!

This StackOverflow answer is written for C++ but does a really good job explaining what's happening here. I'd recommend giving it a read: https://stackoverflow.com/a/45539066/124538

2 Likes

The HashMap type will not automatically decrease the size of the allocation, but you can tell it to do so with shrink_to_fit. Additionally the memory is freed when it goes out of scope.

2 Likes

thank u!!I have see this answer, it is really helpful!

yes, i know that, it will free, but not return to os, just in rust virtual memory。
thanks for your kindness ..^ _ ^..