The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Memory usage in Solaris - memory not freed? gewurtz SUN Solaris 3 01-06-2009 12:06 PM
malloc gives the same memory to two different nodes. How to deal with it? cdbug High Level Programming 2 01-04-2009 09:01 PM
mv files still in use - space not freed apra143 Filesystems, Disks and Memory 4 01-08-2008 07:47 PM
Memory allocated baanprog SUN Solaris 1 05-04-2006 11:06 AM
malloc rajashekaran High Level Programming 2 09-30-2002 06:24 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-31-2009
karthiktceit karthiktceit is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 8
Why memory allocated through malloc should be freed ?

Actually for a process to run it needs text, stack , heap and data segments. All these find a place in the physical memory.
Out of these 4 only heap does exist after the termination of the process that created it. I want to know the exact reason why this happens. Also why the other process need to access the heap of my process. If the other processes compete for the heap that my process has consumed then there wont be need of an IPC mechanism (like my process will put the information in the heap and give that address to the peer process to have the info shared) ...
My question is really simple, I am not freeing the text , data and stack segments used by my process, then y should i do it for heap alone?
  #2 (permalink)  
Old 08-31-2009
jlliagre jlliagre is online now Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,483
All of a process allocated memory should be made available after it dies otherwise the OS will refuse to launch after some period of time.
What OS are you using ?
How are you measuring the memory usage ?
  #3 (permalink)  
Old 08-31-2009
karthiktceit karthiktceit is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 8
I am using fedora 6
But y u say
"All of a process allocated memory should be made available after it dies otherwise the OS will refuse to launch after some period of time"

After a process dies y should its address space be avilable ? y OS wont launch when a process dies ?
I dont understand the point u try to make..
  #4 (permalink)  
Old 08-31-2009
jlliagre jlliagre is online now Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,483
Please use English. It's quite boring for non native speaker (and probably for native ones) to decrypt non standard English.

Also, please answer the second question I asked.

After a process dies, all of its address space should be made available otherwise that would be a memory leak. Memory leaks always end in some crash or denial of service which is particularly unwanted for an operating system.
  #5 (permalink)  
Old 08-31-2009
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
Quote:
After a process dies y should its address space be avilable ? y OS wont launch when a process dies ?
jlliagre didn't mean to say OS will not launch after a process has died, which translates OS won't launch once I issue a clear command.

The point that lies here is - if each and every process that have got poor memory allocation and deallocation within it, will start creating memory leaks ( try hitting memory leak in any SEs - good links will be provided ), slowly it will be using all of the available memory depriving any new process to be booted or loaded due to insufficient memory.
  #6 (permalink)  
Old 08-31-2009
karthiktceit karthiktceit is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 8
I understand that, But any part of the process's address space being leaked leads to a memory leak. But stack, data and text segments' memory is not at all leaked. Beacuse they are freed by the OS itself when a process terminates. But Heap alone needs to be freed explicitly. why is that so?
I use the "size"" command to find the size of each segment.

---------- Post updated at 07:23 PM ---------- Previous update was at 07:20 PM ----------

I understand your point matrix madhan. But can you expalin why the stack or data or text segment's memory is not leaked at all?
  #7 (permalink)  
Old 08-31-2009
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
Quote:
I understand that, But any part of the process's address space being leaked leads to a memory leak. But stack, data and text segments' memory is not at all leaked. Beacuse they are freed by the OS itself when a process terminates. But Heap alone needs to be freed explicitly. why is that so?
Other segments are allocated by OS, and OS knows to and does a good job of properly deallocating once the process is done. But memory to heap is explicitly allocated via - malloc ( are any memory allocation call ) which means that OS is giving authority to the caller to allocate memory which implicitly means its the onus of the caller to deallocate when not in use / not required.

With more power comes more responsibility
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0