473,695 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<algori thm>
#include<unistd .h>
#include<iostre am>
using namespace std;

//typedef vector <int, __malloc_alloc_ template<0 typIntVec;
//typedef map <long, typIntVec, std::less<long> ,
//__malloc_alloc_ template<0 typLongIntVecMa p;
typedef vector <inttypIntVec ;
typedef map <long, typIntVec typLongIntVecMa p;

int main ()
{
typLongIntVecMa p tMap;

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

cout<<"BEGIN EXECUTION."<<fl ush;

typLongIntVecMa p::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<<"."<<flus h;
sleep(1);
}
}

cout<<endl<<"ME M 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::iter ator itr =
find(vecRef.beg in(), vecRef.end(), i);
if(itr != vecRef.end())
{
vecRef.erase(it r);
}
}

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

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

Jul 21 '06 #1
3 2028

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::iter ator itr =
find(vecRef.beg in(), vecRef.end(), i);
if(itr != vecRef.end())
{
vecRef.erase(it r);
}
}
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::iter ator itr =
find(vecRef.be gin(), vecRef.end(), i);
if(itr != vecRef.end())
{
vecRef.erase(it r);
}
}
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::iter ator itr =
find(vecRef.beg in(), vecRef.end(), i);
if(itr != vecRef.end())
{
vecRef.erase(it r);
}
}
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
2299
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 as below.
11
2096
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
1462
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); if (value == 0) fatal ("virtual memory exhausted");
35
5497
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 indeed fixed. Being in a hurry, I originally used a q&d hack...: if sys.platform in ('linux2', 'darwin'): def _memsize(): """ this function tries to return a measurement of how much memory this process is consuming, in some arbitrary unit...
4
36531
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 or delete keyword to do so. Also I don't want to wait for the GC to be called.
1
300
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 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.
171
4874
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
2001
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 takes me a lot of time (since i have to loop determining the data type and freeing it). Is there any idea to free all the contents of the structure in shortest possible time.
3
1583
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
4714
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
8572
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8984
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8847
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8825
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4340
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.