Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > can't free memory for vector in map

Reply
Thread Tools

can't free memory for vector in map

 
 
nw
Guest
Posts: n/a
 
      11-15-2008
Hi all,

I have an issue where memory doesn't appear to be being freed from a
map containing vectors. I'm trying to use the swap trick to do this,
but my memory usage (as reported by VSize on Linux) doesn't seem to go
down.

The the following code segment signals is a map of vector<int>s.
identifier is a string index in to this.

vector<vector<int> > mst;

(signals.find(identifier)).second).clear();

(signals.find(identifier)).second).swap(mst);
(signals.find(identifier)).second).erase(identifie r);


I would imagine the clear is not required and I've tried with and
without it. I'm getting to the point where I'm wondering if this is a
operating system issue rather than anything to do with C++.
 
Reply With Quote
 
 
 
 
nw
Guest
Posts: n/a
 
      11-15-2008

> It's both, or neither. The operating system can tell you how much
> memory it has provided for an application, but it can't tell you how
> much of that memory is actually being used and how much is being held
> in reserve by the application.


By "held in reserve" do you mean by vector/map or something lower
level?

My assumption in this case is that the swap trick should forcibly free
the underlying storage and that the map erase should call the
destructor
of the map anyway. But perhaps what you're saying is that the free
doesn't
actually return the memory to the operating system, in which case is
there
a standard way to forcibly return it to the OS. In my application it
would
be useful if this could be claimed by other processes.
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      11-15-2008
nw wrote:
>> It's both, or neither. The operating system can tell you how much
>> memory it has provided for an application, but it can't tell you
>> how much of that memory is actually being used and how much is
>> being held in reserve by the application.

>
> By "held in reserve" do you mean by vector/map or something lower
> level?
>
> My assumption in this case is that the swap trick should forcibly
> free the underlying storage and that the map erase should call the
> destructor
> of the map anyway. But perhaps what you're saying is that the free
> doesn't
> actually return the memory to the operating system, in which case is
> there
> a standard way to forcibly return it to the OS. In my application it
> would
> be useful if this could be claimed by other processes.


The memory allocation layer of the standard library can hold on to the
memory allocated. There might not be any way to release parts of the
memory to the OS. There might not even be an OS at all!

The language standard has no requirements here.


Bo Persson


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-16-2008
On Nov 15, 7:48*pm, nw <new...@googlemail.com> wrote:
> > It's both, or neither. The operating system can tell you how
> > much memory it has provided for an application, but it can't
> > tell you how much of that memory is actually being used and
> > how much is being held in reserve by the application.


> By "held in reserve" do you mean by vector/map or something
> lower level?


Yes. Concerning the default allocator, the standard says
that "the storage is obtained by calling :perator new(size_t),
but it is unspecified when or how often this function is
called." (Presumable, the same holds for freeing storage.) The
implementation of the standard library thus has the right to
keep storage for later use in a container. And of course, the
standard doesn't really say anything about the actual behavior
of :perator delete. (It says that it shall "deallocate the
storage referenced by the pointer", but it doesn't really say
what it means by "deallocate", except to state that any further
use of the pointer value is undefined. Presumably,

void operator delete( void* ) {}

is a technically legal implementation, although one could
probably complain on QoI grounds.

> My assumption in this case is that the swap trick should
> forcibly free the underlying storage and that the map erase
> should call the destructor of the map anyway. But perhaps what
> you're saying is that the free doesn't actually return the
> memory to the operating system,


The destructor of a standard container returns the memory to
its allocator. That's all that is guaranteed. There's no
guarantee that the allocator call operator delete(), and there's
no guarantee that operator delete() does anything if it does.

> in which case is there a standard way to forcibly return it to
> the OS.


You generally can't, unless you provide your own allocator.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
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 to find out memory occupied by vector, set, or map? Lambda C++ 2 05-20-2008 09:13 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57