Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Map erase does not free memory

Reply
Thread Tools

Map erase does not free memory

 
 
Ged85
Guest
Posts: n/a
 
      02-07-2013
Hi there. I have a problem that is sticked in my mind by days. I hope you can help.

This snippet of code:

map<unsigned long,unsigned long> foo_map;
for (int i = 0;i<K;i++)
foo_map[i] = i;

cout<<"done("<<foo_map.size()<<")"<<endl;
cin>>stop;

cout<<"start empting"<<endl;
for (int i = 0;i<K;i++)
foo_map.erase(i);
cout<<"done("<<foo_map.size()<<")"<<endl;
cin>>stop;

Should empty the map container.

In fact, the size that I go to print turns to be 0. Instead, when I check the OS memory it doesn't change nothing!

As K is huge (order of 10^9), I need that the memory allocated is freed when I erase an element.

Can you give me some snippets, hints, pages, links where I can find some help?

I really thank you all.

Best regards,
G.

 
Reply With Quote
 
 
 
 
Anand Hariharan
Guest
Posts: n/a
 
      02-07-2013
On Feb 7, 7:56*am, Ged85 <(E-Mail Removed)> wrote:
> Hi there. I have a problem that is sticked in my mind by days. I hope youcan help.
>

(...)
> Can you give me some snippets, hints, pages, links where I can find some help?
>


Take a look at Effective STL by Meyers.

For your immediate problem, you want to use std::map::swap

- Anand
 
Reply With Quote
 
 
 
 
Gerald Breuer
Guest
Posts: n/a
 
      02-07-2013
Am 07.02.2013 14:56, schrieb Ged85:

> In fact, the size that I go to print turns to be 0. Instead, when I check the OS memory it doesn't change nothing!


That's because malloc obtains its memory from the operating system in
multiples of pages which often can't easily returned to the operating
system for different reasons. But the memory is available for your
application after being freed.


 
Reply With Quote
 
Melzzzzz
Guest
Posts: n/a
 
      02-07-2013
On Thu, 07 Feb 2013 15:19:04 +0000, Scott Lurndal wrote:

>
> I'd allocate a pool of anonymous memory with mmap() on a POSIX system
> and free it with munamp() when no longer needed.


That is what malloc does anyway...


 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      02-07-2013
Ged85 <(E-Mail Removed)> writes:

[...]
> map<unsigned long,unsigned long> foo_map;
> for (int i = 0;i<K;i++)
> foo_map[i] = i;

[...]
> for (int i = 0;i<K;i++)
> foo_map.erase(i);

[...]
> As K is huge (order of 10^9), I need that the memory allocated is
> freed when I erase an element.


If you're on Linux, see malloc_trim() and friends.

-- Alain.
 
Reply With Quote
 
Ged85
Guest
Posts: n/a
 
      02-07-2013
On Thursday, February 7, 2013 4:19:04 PM UTC+1, Scott Lurndal wrote:
> Ged85 <(E-Mail Removed)> writes:
>
> >Hi there. I have a problem that is sticked in my mind by days. I hope you can help.

>
>
>
> You don't specify which operating system or compiler you are using. It is difficult
>
> to offer any substantial advice absent that information.
>
>
>
> You could have used map::clear() instead of looping and calling map::erase.
>
>
>
> You could simply "delete foo_map;". That would ensure that the container
>
> memory is returned to the application heap, but there is no guarantee that the
>
> application heap allocator (e.g. malloc()) will return the free memory to
>
> the operating system (it generally doesn't).
>
>
>
> I'd allocate a pool of anonymous memory with mmap() on a POSIX system and free it
>
> with munamp() when no longer needed.
>
>
>
> scott


I'm using ubuntu 10.04 with gcc 4.4.3.

The problem is that I'm writing a network simulator. Simulations are fairly long, and I use maps for implementing its critical data structures.

Most of these structures take just few items, but each time that I store a new key/value pair ,deleting some other one, the memory is not deallocated. So maps keep growing filling all my 24GB server memory....

May you be clearer about mmmap()? You're suggesting a custom map implementation?

Thank anyway.

Regards,
G.
 
Reply With Quote
 
Ged85
Guest
Posts: n/a
 
      02-07-2013
On Thursday, February 7, 2013 4:28:58 PM UTC+1, Alain Ketterlin wrote:
> Ged85 <(E-Mail Removed)> writes:
>
>
>
> [...]
>
> > map<unsigned long,unsigned long> foo_map;

>
> > for (int i = 0;i<K;i++)

>
> > foo_map[i] = i;

>
> [...]
>
> > for (int i = 0;i<K;i++)

>
> > foo_map.erase(i);

>
> [...]
>
> > As K is huge (order of 10^9), I need that the memory allocated is

>
> > freed when I erase an element.

>
>
>
> If you're on Linux, see malloc_trim() and friends.
>
>
>
> -- Alain.


How can I exploit malloc_trim() function? Custom allocator for map classes?

G.
 
Reply With Quote
 
Gerald Breuer
Guest
Posts: n/a
 
      02-07-2013
Am 07.02.2013 16:28, schrieb Alain Ketterlin:

> If you're on Linux, see malloc_trim() and friends.


It would need only a single allocation somewhere at the end of the
heap to prevent malloc_trim from returning any substantial amount
of memory.

 
Reply With Quote
 
Gerald Breuer
Guest
Posts: n/a
 
      02-07-2013
Am 07.02.2013 16:32, schrieb Ged85:

> Most of these structures take just few items, ...



Memory fragmentation can lead to memory freed with delete
or free not to be reused by later allocations, but usually
such a blowup shouldn't happen. Can it be that you missed
something of your code to post?
 
Reply With Quote
 
Melzzzzz
Guest
Posts: n/a
 
      02-07-2013
On Thu, 07 Feb 2013 07:32:08 -0800, Ged85 wrote:

>
> Most of these structures take just few items, but each time that I store
> a new key/value pair ,deleting some other one, the memory is not
> deallocated. So maps keep growing filling all my 24GB server memory....


Are you sure you don't have memory leak somewhere?
map should reuse memory , it would grow to some extent but 24gb?
 
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
how does map take care invalid range for erase()? newbie C++ 2 07-27-2007 06:21 PM
Map erase memory problem Milind C++ 1 06-15-2006 11:39 AM
erase vs. erase al.cpwn@gmail.com C++ 7 03-30-2006 11:45 AM
How do i erase all information in my orkut and erase the link to the orkut account? mountbatten@gmail.com Computer Support 1 10-31-2005 12:03 AM
How do I erase a file that wont let me erase it? Dale Custer Computer Support 4 07-06-2003 09:48 AM



Advertisments