niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
Where is this code not freeing memory ?
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
Thomas J. Gritzan
*nix forums Guru Wannabe


Joined: 09 Oct 2005
Posts: 124

PostPosted: Fri Jul 21, 2006 1:51 pm    Post subject: Re: Where is this code not freeing memory ? Reply with quote

joosteto@gmail.com schrieb:
Quote:
wolverine wrote:

Ah, there does seem to be real-non-freed memory:
for (long j=1; j<200000; j++)
{
typIntVec vec;

Here, you construct IntVec.
for(int i=0; i<100; i++)
{
vec.push_back(i);
}
Here, you construct 100 int's and put them in the IntVec

tMap[j] = vec;

Here you create a map entry

}
for (long j=1; j<200000; j++)
{

for(int i=0; i<100; i++)
{
typIntVec::iterator itr =
find(vecRef.begin(), vecRef.end(), i);
if(itr != vecRef.end())
{
vecRef.erase(itr);
}
}
You destruct the 100 int's -- but where do you destruct the IntVec?
Where do you destruct the entry in the map?


In the destructor. It's called RAII.

--
Thomas
Back to top
joosteto@gmail.com
*nix forums beginner


Joined: 09 Jul 2006
Posts: 20

PostPosted: Fri Jul 21, 2006 10:20 am    Post subject: Re: Where is this code not freeing memory ? Reply with quote

wolverine wrote:

Ah, there does seem to be real-non-freed memory:
Quote:
for (long j=1; j<200000; j++)
{
typIntVec vec;

Here, you construct IntVec.
Quote:

for(int i=0; i<100; i++)
{
vec.push_back(i);
}
Here, you construct 100 int's and put them in the IntVec


Quote:
tMap[j] = vec;

Here you create a map entry

Quote:
}
for (long j=1; j<200000; j++)
{

for(int i=0; i<100; i++)
{
typIntVec::iterator itr =
find(vecRef.begin(), vecRef.end(), i);
if(itr != vecRef.end())
{
vecRef.erase(itr);
}
}
You destruct the 100 int's -- but where do you destruct the IntVec?

Where do you destruct the entry in the map?
Back to top
jithoosin
*nix forums beginner


Joined: 21 Jul 2006
Posts: 6

PostPosted: Fri Jul 21, 2006 9:39 am    Post subject: Where is this code not freeing memory ? Reply with quote

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.wss?rs=994&context=SSSHAD&dc=DB520&uid=swg21160634&loc=en_US&cs=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;

//inserting 100 elements into the vector
for(int i=0; i<100; i++)
{
vec.push_back(i);
}

//put the vector into the map
tMap[j] = vec;

//simply putting a sleep so that we could see the increase in memory
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++)
{
//get back a reference to the vector inside the map
typIntVec& vecRef = tMap[j];

//delete every element inside the vector
for(int i=0; i<100; i++)
{
typIntVec::iterator itr =
find(vecRef.begin(), vecRef.end(), i);
if(itr != vecRef.end())
{
vecRef.erase(itr);
}
}

//sleeping simple so that we could see the decrease in memory
if(j % 40000 == 0)
{
cout<<"."<<flush;
sleep(1);
}
}

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Thu Nov 20, 2008 7:48 am | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts postfix out of memory error - please help metind Postfix 0 Mon Sep 11, 2006 1:54 am
No new posts Non IBM memory in p630 Ron AIX 0 Fri Jul 21, 2006 2:05 pm
No new posts database Share Memory Limit (2 GB ) in a Instance is tota... sadanjan@gmail.com IBM DB2 0 Fri Jul 21, 2006 12:57 pm
No new posts Where is this code not freeing memory ? jithoosin C++ 1 Fri Jul 21, 2006 9:27 am
No new posts Where is this code not freeing memory ? jithoosin C++ 1 Fri Jul 21, 2006 9:12 am

Books | Online Advertising | Debt Consolidation | Property in Spain | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1666s ][ Queries: 20 (0.0739s) ][ GZIP on - Debug on ]