By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

memory usage problem

P: n/a
I have a problem about memory usage. The code is as below:
#include <list>

int main(int argc, char* argv[])
{
{
std::list<void*ptr_list;
{
for (int i=0; i<9368; i++)
{
size_t size = 10044;
void* p = ::malloc (size);
ptr_list.push_back (p);
}

for (std::list<void*>::iterator i=ptr_list.begin(); i!= ptr_list.end();
i++)
{
void* p=(*i);
::free (p);
}
}
ptr_list.clear();
}
return 0;
}

When all elements in list are freed, the application still have about 30M
memory. After clear function is called, most of memory is freed. However,
the application will have 300k more than the beginning. I think all
allocated memory should be released after list is cleared. The question
is:
1. Why does list still have 30M even if all elements are freed?
2. Why does this program take up 300k more than the beginning?

Test environment:
Windows XP
VS 2005
Mar 21 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mar 21, 11:59 am, fishscience <jacky...@126.comwrote:
I have a problem about memory usage. The code is as below:
#include <list>

int main(int argc, char* argv[])
{
{
std::list<void*ptr_list;
{
for (int i=0; i<9368; i++)
{
size_t size = 10044;
void* p = ::malloc (size);
ptr_list.push_back (p);
}

for (std::list<void*>::iterator i=ptr_list.begin(); i!= ptr_list.end();
i++)
{
void* p=(*i);
::free (p);
}
}
ptr_list.clear();
}
return 0;

}

When all elements in list are freed, the application still have about 30M
memory. After clear function is called, most of memory is freed. However,
the application will have 300k more than the beginning. I think all
allocated memory should be released after list is cleared. The question
is:
1. Why does list still have 30M even if all elements are freed?
2. Why does this program take up 300k more than the beginning?

Test environment:
Windows XP
VS 2005
In standard C++ terms, I would expect your code to behave as you are
expecting: all of the malloc'ed memory is freed in your for loop, and
the list's overhead is freed with list::clear(). However, measuring
memory usage can be tricky and deceptive (you're not using the task
manager for this purpose, are you?) -- not to mention platform-
specific, which makes it off-topic here (http://www.parashift.com/c++-
faq-lite/how-to-post.html#faq-5.9). For more direct help, you'll want
to ask in a group dedicated to your platform or tools.

Also, consider using an RAII container such as vector instead of
explicitly using malloc and free (http://www.parashift.com/c++-faq-
lite/containers.html#faq-34.1), or at least use a smart pointer list
std::tr1::shared_ptr to automatically clean up.

Cheers! --M

Mar 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.