473,406 Members | 2,619 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,406 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 2006

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.