what is this memory usage

C

CMOS

consider the below code. it just creates a map and inserts 51000000
elements. when i check the memory usage , it shows an increase of
memory after the loop. the increase is around 2mb. i used "ps aux"
command and use the VSZ value.
why is this happening? i dont see any memory leaks as nothing is
created in heap/free store.
is this some kind of a memory fragmentation issue?

// memory ckeck 1 goes here
{
std::map<std::string, int> mm;
for(int i=0; i<51000000; ++i)
{
std::string s;
s = i;
mm = i;
}
}

// memory ckeck 2 goes here, and there is an increase in memory
 
T

trojanfoe

consider the below code. it just creates a map and inserts 51000000
elements. when i check the memory usage , it shows an increase of
memory after the loop. the increase is around 2mb. i used "ps aux"
command and use the VSZ value.
why is this happening? i dont see any memory leaks as nothing is
created in heap/free store.
is this some kind of a memory fragmentation issue?

// memory ckeck 1 goes here
{
std::map<std::string, int> mm;
for(int i=0; i<51000000; ++i)
{
std::string s;
s = i;
mm = i;
}

}

// memory ckeck 2 goes here, and there is an increase in memory


Isn't that right? You have created objects between memcheck 1 and 2,
and so should an increase in memory use.
 
V

Victor Bazarov

trojanfoe said:
consider the below code. it just creates a map and inserts 51000000
elements. when i check the memory usage , it shows an increase of
memory after the loop. the increase is around 2mb. i used "ps aux"
command and use the VSZ value.
why is this happening? i dont see any memory leaks as nothing is
created in heap/free store.
is this some kind of a memory fragmentation issue?

// memory ckeck 1 goes here
{
std::map<std::string, int> mm;
for(int i=0; i<51000000; ++i)
{
std::string s;
s = i;
mm = i;
}

}

// memory ckeck 2 goes here, and there is an increase in memory


Isn't that right? You have created objects between memcheck 1 and 2,
and so should an increase in memory use.


The map is local to the block, as soon as the block closes, the map
should be destroyed and the memory should be released.

To the OP: checking the memory using OS specific tools is OS specific,
and any results cannot be explained in terms of C++ language. If you
are concerned with leak or fragmentation, use the proper tools to find
out whether those conditions are present, but we in comp.lang.c++ do
not have a way to help you.

Often the OS gives the running process more memory as it needs it, but
does not claim it back when the process releases the memory internally
(like when calling 'free' for, or 'delete'ing, a pointer). You need
to use other means to verify or dismiss your doubts in the correctness
of the memory consumption of your process.

V
 
M

Markus Moll

Hi
consider the below code. it just creates a map and inserts 51000000
elements.

Um... no. It inserts at most 2^CHAR_BITS elements (Probably 256). Possibly
fewer if your char is signed.
when i check the memory usage , it shows an increase of
memory after the loop. the increase is around 2mb. i used "ps aux"
command and use the VSZ value.
why is this happening? i dont see any memory leaks as nothing is
created in heap/free store.
is this some kind of a memory fragmentation issue?

// memory ckeck 1 goes here
{
std::map<std::string, int> mm;
for(int i=0; i<51000000; ++i)
{
std::string s;
s = i;

i is implicitly converted to a char here. The conversion is
implementation-defined if char is signed. Otherwise the value will be i
modulo 2^CHAR_BITS.
mm = i;
}
}

// memory ckeck 2 goes here, and there is an increase in memory


See the comp.lang.c FAQ: http://c-faq.com/malloc/freetoOS.html

The same applies to C++.

Markus
 
V

Victor Bazarov

CMOS said:
thanks for the info.
im wondering whether it is possible to forcely release allocated
memory to the os.

If you find out, how do you think it would help you? Well, if you
are convinced that it's going to help, ask about it in the newsgroup
dedicated to your OS.

V
 
C

Cavancn

consider the below code. it just creates a map and inserts 51000000
elements. when i check the memory usage , it shows an increase of
memory after the loop. the increase is around 2mb. i used "ps aux"
command and use the VSZ value.
why is this happening? i dont see any memory leaks as nothing is
created in heap/free store.
is this some kind of a memory fragmentation issue?

// memory ckeck 1 goes here
{
std::map<std::string, int> mm;
for(int i=0; i<51000000; ++i)
{
std::string s;
s = i;
mm = i;
}

//Add new line here and have a try
mm.swap(std::map said:
}

// memory ckeck 2 goes here, and there is an increase in memory

Cavan
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top