help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: std::List doesn't releases the memory ...


From: Pascal J. Bourguignon
Subject: Re: std::List doesn't releases the memory ...
Date: Mon, 06 Jul 2009 10:05:43 +0200
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

RV <rahul.vakil@gmail.com> writes:

> Hi,
>
> I have compiled the following source code on SuSE linux with g++
> compiler version 3.4.3.
>
> #include <iostream>
> #include <list>
>
> void PopulateList(std::list<int> & li, int numitr)
> {
>         for(int i = 0; i < numitr; i++){
>                 li.push_back(i);
>         }
> }
>
> void testList()
> {
>         char ch;
>         int numitr;
>
>                 std::list<int> li;
>                 std::cout << "Enter any charater to continue : Before
> Populating std::list<int> : take memory reading: ";
>                 std::cin >> ch;
>                 std::cout << "How many iteration: ";
>                 std::cin >> numitr;
>
>                 PopulateList(li, numitr);
>
>                 std::cout << "Press any charater to continue : After
> Populating std::list<int> : take memory reading: ";
>                 std::cin >> ch;
>
>                 li.clear();
>
>                 std::cout << "Press any character to continue : After
> clearing std::list<int> : take memory reading: ";
>                 std::cin >> ch;
> }
>
> int main()
> {
>         while(1) {
>                 testList();
>                 std::cout << "==>\n";
>                 std::cout << "==> End of while\n";
>                 std::cout << "==>\n";
>         } // End of While
> }
>
> OBSERVATION:
>
> The multiple iteration of while loop doesn't release the memory
> occupied by the std::list

Yes it does.  The memory is freed.  If you try to allocate the list
again, you will see that no more memory is asked from the OS.

What you are observing is not the occupied / free memory in the
process memory space, but the size of the process memory space
allocated to the process by the OS.  The OS doesn't lose its time, and
more importantly, doesn't make the OS and all the other lose their
time, in returning the memory to the OS, while the application is
still running and may still need that memory.


> Is this a bug with std::List or incorrect usage of it?

No.


> The same behaviour is not observed with std::vector?

Yes.


> Does anyone knows the solution for this?

This is not a problem you want to solve.


-- 
__Pascal Bourguignon__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]