LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-08-2011, 10:44 AM   #1
Zssfssz
Member
 
Registered: Sep 2011
Location: Las Vegas!
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339

Rep: Reputation: Disabled
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-08-2011, 11:21 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Gentoo
Posts: 15,438
Blog Entries: 2

Rep: Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001
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.
 
Old 11-08-2011, 11:58 AM   #3
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,055

Rep: Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104
Quote:
Originally Posted by TobiSGD View Post
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.

Last edited by johnsfine; 11-08-2011 at 12:02 PM.
 
3 members found this post helpful.
Old 11-08-2011, 12:18 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Gentoo
Posts: 15,438
Blog Entries: 2

Rep: Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001Reputation: 4001
Quote:
Originally Posted by johnsfine View Post
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.
 
Old 11-08-2011, 03:17 PM   #5
Zssfssz
Member
 
Registered: Sep 2011
Location: Las Vegas!
Distribution: Ubuntu n' Flavors, ReactOS, MINIX3, FreeDOS, Arch
Posts: 339

Original Poster
Rep: Reputation: Disabled
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)
 
Old 11-08-2011, 03:46 PM   #6
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,055

Rep: Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104
Quote:
Originally Posted by Zssfssz View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Heap overflow:.. primenu Programming 8 02-01-2010 04:30 PM
Is there any heap compaction in C/C++? hs_linux Programming 2 12-14-2009 08:04 AM
Heap of Problems puesdo Linux - Newbie 1 07-25-2009 10:39 AM
Min Heap ShaqDiesel Programming 5 03-26-2008 05:30 PM
heap or stack yashwantpinge Programming 1 03-17-2006 07:25 AM


All times are GMT -5. The time now is 10:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration