Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team, i have tries to analysis the memcheck throughout while insertion of new elements in list, map, vector anything. am getting high memory utilization at particular PID in linux. After the list object as clearing, still am seeing the same memory utilization.

#include<bits/stdc++.h>
using namespace std;
#include<unistd.h>

int main()
{
   list<string> l1;

   for(int i=0;i<10000000;++i)
   {
      stringstream ss;
      ss << "000000000000000" << i << "__" << i << "00000000000000000000";
      l1.push_back(ss.str());
   }

   cout<<"Before Size : "<<l1.size()<<endl;

   sleep(5);

   l1.clear();
   cout<<"After Size : "<<l1.size()<<endl;

   getchar();

}


compliation : g++ Design.cpp -o Design

seen high memory in linux terminal:
top -b | grep -i Design
19081 issue 20   0 1231.4m 1.192g   1.0m S  0.0 32.2   0:13.67 Design
19081 issue 20   0 1231.4m 1.192g   1.0m S  0.0 32.2   0:13.67 Design


What I have tried:

Tried with pointer object for map, but still memory is not reduced.
Posted
Updated 20-May-23 2:56am

What did you expect?
You are creating 10,000,000 streams, and adding them all to a list:
list<string> l1;

for(int i=0;i<10000000;++i)
{
   stringstream ss;
   ss << "000000000000000" << i << "__" << i << "00000000000000000000";
   l1.push_back(ss.str());
}

Even if you clear the list when you are finished with it, the streams are still in existence, and the memory they took up will not be released until they all go out of scope - which is when the function ends. See here: Resource acquisition is initialization - Wikipedia[^]
 
Share this answer
 
Comments
CPallini 18-May-23 7:38am    
Nope, each stream goes out of scope at the end of their iteration in the for loop.
clear() in STL containers does not re-shrink the allocated storage. If you call capacity(), you will get the real allocated storage size on an emptied container. If you must release the allocated storage you would need to call swap() function with temporary container. Swapping between the two containers will release all memory.

The reason the STL does not release the allocated storage after clear is called is efficiency. Internally STL vectors using heap storage and it is very expensive operation to constantly allocate and deallocate on the heap. For example if you were to reuse that std::list container again later in code, there will be no new allocations done if the allocated memory is already large enough. This speeds up code in orders of magnitude versus stating off again with an empty storage. This is why if you use STL containers in the code, you should re-use them again because it would not need to allocate heap 2nd time.
 
Share this answer
 
v2
Comments
k5054 20-May-23 11:33am    
capacity is only available for std::vector. Furthermore, using swap does not release process allocated memory, at least not for std::list. Nor does the object going out of scope release memory allocated to the process. For example, suppose we put the body of OP's main function in a separate function, and call that from main. ps will report the same data resident size at the end of the function, and just before main returns. If we call the allocating function twice, the first time allocating a large number of list elements, and the second time with a small number of elements, the drs does not change between the first and second calls.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900