How can I make the STL memory cache clear?

I have a class that has a field of type unordered_map . I create one instance of this object in my application, which is wrapped in shared_ptr . The object has a lot of memory, and I want to delete it as soon as I have finished using it. However, resetting the pointer only frees up a small portion of the memory occupied by the object. How to make a program free all the memory occupied by an object?

The following breadboard program reproduces my problem. Loop trash exists only to give me enough time to observe the memory used with top . The destructor is called immediately after reset() . In addition, right away, used memory drops from about 2 GB to 1.5 GB.

 #include <iostream> #include <memory> #include <unordered_map> using namespace std; struct A { ~A() { cerr << "Destructor" << endl; } unordered_map<int, int> index; }; int main() { shared_ptr<A> a = make_shared<A>(); for (size_t i = 0; i < 50000000; ++i) { a->index[2*i] = i + 3; } // Do some random work. for (size_t i = 0; i < 3000000; ++i) { cout << "First" << endl; } a.reset(); // More random work. for (size_t i = 0; i < 3000000; ++i) { cout << "Second" << endl; } } 

Compiler: g ++ 4.6.3.

+6
source share
2 answers

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.

+5
source

There is no guarantee that your application will free memory back to the OS. It is still available for your application, but the OS cannot return it for general use until your application exits.

+4
source

Source: https://habr.com/ru/post/947680/


All Articles