LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Regarding the heap. (http://www.linuxquestions.org/questions/programming-9/regarding-the-heap-912492/)

Zssfssz 11-08-2011 11:44 AM

Regarding the heap.
 
The memory allocated to the heap in a program gets wiped/deleted when the computer shuts Off right? I'm makin a program that uses it but its still in the testing state so some of the heap allocated remains every now and again.

TobiSGD 11-08-2011 12:21 PM

The memory allocated to the heap should be freed when you close your application, regardless if you reboot or not. It is up to your program to free up used ressources, at least as long as you are not using a language with automatic garbage collection, IIRC.

johnsfine 11-08-2011 12:58 PM

Quote:

Originally Posted by TobiSGD (Post 4518961)
The memory allocated to the heap should be freed when you close your application, regardless if you reboot or not. It is up to your program to free up used ressources, at least as long as you are not using a language with automatic garbage collection,

Because of the way you phrased that, I can't tell whether you are giving the correct answer or its opposite.

When your program exits, the OS will reclaim all of the memory allocated for that program's heap. It doesn't matter if the program has some memory leak and fails to release some of its heap. The OS takes it back anyway when the program exits.

If the OS is shut down or crashes, all the memory allocations are effectively released regardless of whether those processes exited properly. The OS starts fresh when rebooted. But make sure you know the difference between OFF and HIBERNATING. When the computer is hibernating, processes keep their allocated memory.

While your process is running, assuming the language or program doesn't have automatic garbage collection, the program is responsible for deleting memory it no longer needs. If it fails to do so, that memory is tied up as long as the process exists. But will be released anyway when the process is gone.

TobiSGD 11-08-2011 01:18 PM

Quote:

Originally Posted by johnsfine (Post 4518996)
Because of the way you phrased that, I can't tell whether you are giving the correct answer or its opposite.

Actually it was the opposite. Thanks for your correction.

Zssfssz 11-08-2011 04:17 PM

Oh man Im used to posting in a C++ fourm. My languige is C++ (der). I am pretty sure it doesnt have automatic grabade collection.
I thought that heap memory would remain allocated until the program ends_it whether or not the program ends.

(I dont know how to word that differently)

johnsfine 11-08-2011 04:46 PM

Quote:

Originally Posted by Zssfssz (Post 4519161)
I am pretty sure it doesnt have automatic grabade collection.

Correct.

Quote:

I thought that heap memory would remain allocated until the program ends_it whether or not the program ends.

(I dont know how to word that differently)
Better wording would have helped.

But anyway, it is what you thought (past tense). Now I hope you understand heap memory is freed back to the OS when the program exits.

If the program frees memory but does not exit, that memory might be freed back to the OS so other programs can use it, but typically not. Typically memory your program frees goes into a pool within that process that only that process can reuse. That memory is freed back to the OS when the process exits.

If you allocate a very large chunk of memory then free it, it probably goes directly back to the OS when freed.

If you allocate many small chunks of memory, then free them, the large chunk created by consolidating contiguous small chunks of free memory could be released back to the OS, but probably won't be.


All times are GMT -5. The time now is 09:58 AM.