473,385 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Where is this code not freeing memory ?

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 <inttypIntVec;
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;
}

Jul 21 '06 #1
3 2003

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?

Jul 21 '06 #2
jo******@gmail.com schrieb:
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
Jul 21 '06 #3

Thomas J. Gritzan wrote:
jo******@gmail.com schrieb:
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.
In who's destructor? Do you mean tMap's destructor? Then we agree, but
that's not what the poster asked.
tMap's destructor is called when main() finishes, and the poster
wondered why de memory wasn't freed *before* main() finishes (and thus
before tMap is destructed).

The cout statements show that the poster thinks that before the end of
main(), there is a "MAP with no enteries". At that point however, no
entry from tMap has been erased, and thus no IntVe has been destructed.
--
Thomas
Jul 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: binaya | last post by:
Dear all, Say I allocate a block of memory using the command malloc.I have a question. Can I deallocate certain portion of it only during runtime ? For eg: Say I allocate 5 pointers to int...
11
by: AC | last post by:
Is the following code valid? #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int a; char s;
7
by: grocery_stocker | last post by:
Given the following snippet of code: (taken from http://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html) void * xmalloc (size_t size) { register void *value = malloc (size);...
35
by: Alex Martelli | last post by:
Having fixed a memory leak (not the leak of a Python reference, some other stuff I wasn't properly freeing in certain cases) in a C-coded extension I maintain, I need a way to test that the leak is...
4
by: Atul Sureka | last post by:
Hi, I want to free the object memory in C# - like we do using 'delete' keyword in C++. Lets say I have an object of some class and I want to explicitly free the memory. C# do not have any free...
1
by: wolverine | last post by:
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...
171
by: Raman | last post by:
Hi All, Here is a small Code, int main(void) { char *p=(char *) malloc(100); strcpy(p,"Test1234567890"); p=p+10; free(p);
11
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it...
3
by: Victor Bazarov | last post by:
jl_post@hotmail.com wrote: Yes, certainly. Actually 'delete' is an operator, not a function. The parens are superfluous. There is really no need for that.
25
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.