The GCC standard library does not have an โSTL memory cacheโ in its default configuration (which almost everyone uses) std::allocator simply calls new and delete , which simply call malloc and free . The malloc implementation (which usually comes from the C library) decides whether to return memory to the OS. If you are not using an embedded / limited system without virtual memory (or you have disabled over-commit), then you probably do not want to return it - let the library do what it wants.
The OS does not need to restore memory; it can easily allocate gigabytes of virtual memory for other applications. Whenever people think that they need to return memory, this is usually because they do not understand how a modern operating system processes virtual memory.
If you really want to force the C library to return memory to the OS, the C library can provide non-standard interceptors for this, for example, for GNU libc you can call malloc_trim(0) to force the very top of the free memory to be returned to the OS, but this will probably make your program slower the next time it needs to allocate more memory because it will have to return it from the OS. See fooobar.com/questions/373375 / ... (and other answers there) for more details.
source share