Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Where is this code not freeing memory ?

Reply
Thread Tools

Where is this code not freeing memory ?

 
 
wolverine
Guest
Posts: n/a
 
      07-21-2006
Hi
Let me first of all tell that this problem is not specific to a
compiler like gcc. It even comes in windows. So please dont regard the
question as off topic.

I am posting a code using stl. I viewed the memory for program
by giving top command
eg: top -d 0.2 -p 'pid'
The pid will be printed out by the program itself.

Now coming to the question. The code is supposed to take around (200000
* 100 * 4 bytes = 76MB) .The top command at halfway point (line 41)
showed around 80MB. Until now it is correct. But after this i start
deleting elements from vector one by one. Then the code is supposed to
take lesser memory. But the top command showed still 80MB.
Why is this ?

I then googled and read in
http://www-1.ibm.com/support/docview...=UTF-8&lang=en
that stl "caches" memory and they gave a work around for it. I tried in
the code. Even that is not working.

Let me tell that valgrind or purify is not showing any leak in the
code. I some how need to free
from stl the memory. Is there any way ? Could any one help me ? I need
a general solution
which is applicable for all stl containers.

The code:


#include <vector>
#include <map>
#include<algorithm>
#include<unistd.h>
#include<iostream>
using namespace std;

//typedef vector <int, __malloc_alloc_template<0> > typIntVec;
//typedef map <long, typIntVec, std::less<long>,
//__malloc_alloc_template<0> > typLongIntVecMap;
typedef vector <int> typIntVec;
typedef map <long, typIntVec> typLongIntVecMap;



int main ()
{
typLongIntVecMap tMap;

cout<<"PID IS:"<<getpid()<<endl;
sleep(2);

cout<<"BEGIN EXECUTION."<<flush;

typLongIntVecMap::iterator tMapItr;
for (long j=1; j<200000; j++)
{
typIntVec vec;
for(int i=0; i<100; i++)
{
vec.push_back(i);
}
tMap[j] = vec;

if(j % 40000 == 0)
{
cout<<"."<<flush;
sleep(1);
}
}

cout<<endl<<"MEM IN STABLE POSITION......."<<endl;
cout<<"NOW IT SHOULD DECREASE";
sleep(2);
for (long j=1; j<200000; j++)
{
typIntVec& vecRef = tMap[j];
for(int i=0; i<100; i++)
{
typIntVec::iterator itr = find(vecRef.begin(), vecRef.end(), i);
if(itr != vecRef.end())
{
vecRef.erase(itr);
}
}

if(j % 40000 == 0)
{
cout<<"."<<flush;
sleep(1);
}
}

cout<<endl<<"MAP WITH NO ELEMENTS IS IN MEM NOW"<<endl;
sleep(2);
return 0;
}


Thanks in advance
Regards
Kiran Pradeep

 
Reply With Quote
 
 
 
 
Joost Witteveen
Guest
Posts: n/a
 
      07-21-2006
On 2006-07-21, wolverine <> wrote:
> Now coming to the question. The code is supposed to take around (200000
> * 100 * 4 bytes = 76MB) .The top command at halfway point (line 41)
> showed around 80MB. Until now it is correct. But after this i start
> deleting elements from vector one by one. Then the code is supposed to
> take lesser memory. But the top command showed still 80MB.
> Why is this ?
>

Because even when a program calls free(), the memory isn't actually
returned to the OS, but rather it stays part of the program memory
pool, just marked as memory available to be used for the next malloc
call.


> Let me tell that valgrind or purify is not showing any leak in the
> code.


That's because there are no leaks!

> I some how need to free from stl the memory


It's not just stl memory. You should also see it if you just use malloc.
(and maybe initialise the memory). However, if you, after the
free/destruction call malloc again, you'll see your memory usage doesn't
go up again, it's using the old memory again.

IIRC this is also the reason firefox seems to use so much memory: if you
at any time visited many big pages (many tabs), and closed all those tabs,
memory usage of firefox still is high.

If you really want to return the memory, maybe looking at how X does it
may help (I believe X does return memory)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is this code not freeing memory ? wolverine C++ 3 07-22-2006 02:01 PM
Where is this code not freeing memory ? jithoosin C++ 1 07-21-2006 12:43 PM
Where is this code not freeing memory ? jithoosin C++ 0 07-21-2006 09:09 AM
tkinter: not freeing memory like I'd expect Benjamin Rutt Python 0 03-30-2006 07:56 PM
memory allocation and freeing memory Rodrigo Dominguez C Programming 11 06-14-2005 11:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57