STL map Unusual behaviour

S

slonial

Hi,

I am using two STL maps as data member for COM server with
VC++6.0.These two maps are unrelated in the sense that they are storing

different data.
first map is of type(6 MB in size)
std::map<long,ISumInfo*>


and second is of type(3 MB in process)
std::map<string,list<string> >


I want to clear the first map but on clearing the map the memory
doesn't come down for process.But as soon as I clear the second map the

memory comes down is equivalent
to size of both the maps.


I am not able to understand that how two maps are related.
For clearing the first map I have released the IsumInfo objects and
destructors are getting invoked properly for clearing the map I have
tried calling
three ways but the result is same.
//m_siteDB.clear();


//m_siteDB.erase(m_siteDB.begin(), m_siteDB.end());


//std::map<long,ISumInfo*>().swap(m_siteDB);


If somebody have faced this issue,Please let me know


Thanx
Sumit
 
N

n2xssvv g02gfr12930

Hi,

I am using two STL maps as data member for COM server with
VC++6.0.These two maps are unrelated in the sense that they are storing

different data.
first map is of type(6 MB in size)
std::map<long,ISumInfo*>


and second is of type(3 MB in process)
std::map<string,list<string> >


I want to clear the first map but on clearing the map the memory
doesn't come down for process.But as soon as I clear the second map the

memory comes down is equivalent
to size of both the maps.


I am not able to understand that how two maps are related.
For clearing the first map I have released the IsumInfo objects and
destructors are getting invoked properly for clearing the map I have
tried calling
three ways but the result is same.
//m_siteDB.clear();


//m_siteDB.erase(m_siteDB.begin(), m_siteDB.end());


//std::map<long,ISumInfo*>().swap(m_siteDB);


If somebody have faced this issue,Please let me know


Thanx
Sumit

The STL containers have built in memory management which reserve memory
for later use. Unless you write your own Allocator the results you are
getting will depend on the STL implementation. It would seem in your
case it's reserved memory for std::map<> use and doesn't immediately
free it up. This helps speed up creating and destroying new map objects
because fewer memory allocation and deallocation calls are required for
these operations.

NOTE!!! Make sure std::map<long,ISumInfo*> pointer within the map object
will not leave memory hanging before destroying the object.

JB
 
S

slonial

Using STL map,list with own customized Allocator really worked it
reduces the memory
immediately.I have downloaded the allocator from web only.

http://www.codeproject.com/vcpp/stl/blockallocator.asp#xx159315xx

I have put lot of hours in this to find out the reason but ur reply
really
helped me in rectifying this problem.

Just in case u can know that how good is the above allocator
implementation
let me know.

Thanx,
Sumit
 
A

AnonMail2005

According to Meyer's Effective STL, you can trim excess capacity in
an STL container by using the swap trick.

// define vector fo MyType
vector <MyType> v;

// add elements to v

// create a temporary vector via the copy constructor then swap it with
// v. the copy constructor sizes the temp vector for the "correct"
// amount of memory (not any excess capacity that may be in v).
vector <MyType> (v).swap (v);

// same thing but clears _and_ minimizes capacity.
vector <MyType> ().swap (v);

The book explains this in more depth including what "correct" is and
how it depends on the STL implementation.

You may want to try that since using a customized Allocator seems like
a bit much to accomplish what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top