Suppose I have a main() function with only one malloc statement allocating say some 1 gb memory. Also say my system has 1 gb of ram.
The program above exits without freeing the memory.
In this case will the 1 gb of heap memory be returned to system on above process termination or has the heap memory permanently leaked and will be available only on system reboot?
I mean if say we start some another process or say the same process again then will they be able to get the 1 gb if heap again?
Last edited by rupeshkp728; 10-25-2012 at 12:49 PM..
Reason: code syntax
All memory allocated by a process is returned to the system when the process terminates.
A very small amount of memory will remain after the process terminates that indicates the status of the terminated process. That memory will be released when the process that started the terminated process either gathers the exit status of its dead child (by calling something like wait()) or terminates.
This User Gave Thanks to Don Cragun For This Post:
Consider this; all malloc() does is either give you memory that already belongs to the process, or change the size of the heap segment so it can give you even more. free() never returns it to the system -- it just puts it in a list so malloc() knows what to recycle next time.
The heap segment ceases to exist when the program quits, just like the code segment, data segment, stack segment, and all the other segments.
free() never returns it to the system -- it just puts it in a list so malloc() knows what to recycle next time.
In the open source world, that hasn't been true for about 10 years. Linux (glibc), OpenBSD, NetBSD, and FreeBSD all return memory to the system under certain circumstances (such as free()ing a massive chunk).
I have no idea how the proprietary libcs behave.
The second half of this post about the exact same topic contains excerpts from a couple of malloc/free implementations, with links to their sources: https://www.unix.com/302717047-post9.html
Thanks all for the reply.
The link HowStuffWorks "The Basics of C Programming" also helps stating:
What happens if I forget to delete a block of memory before the program terminates? When a program terminates, the operating system "cleans up after it," releasing its executable code space, stack, global memory space and any heap allocations for recycling. Therefore, there are no long-term consequences to leaving allocations pending at program termination.
In the open source world, that hasn't been true for about 10 years. Linux (glibc), OpenBSD, NetBSD, and FreeBSD all return memory to the system under certain circumstances (such as free()ing a massive chunk).
This is contrary to what I have read...since the freed memory return to the OS pool only when the process exits. I maybe wrong but this seems to contradict what I read in this book...besides frequent garbage collection by the OS is going to make the kernel sweat. In fact whatever I understood about runtime memory allocation from that book is exactly what Corona688 posted.
This is contrary to what I have read...since the freed memory return to the OS pool only when the process exits. I maybe wrong but this seems to contradict what I read in this book..
Then what you read is either a pedagogical simplification or an oversight. Since the statement, "free() doesn't return memory to the system", is true for 4.4BSD [see Note 1], and since The Design and Implementation of the 4.4BSD Operating System is the predecessor of The Design and Implementation of the FreeBSD Operating System, perhaps the statement originated in the earlier work and was carried over unintentionally.
The first book was published on May 10, 1996. The second on August, 12, 2004. FreeBSD began returning memory (when possible) to the kernel when it adopted Poul-Henning Kamp's phkmalloc back in September 16, 1995, before either book hit the shelves. From malloc.c revision 1.3:
Freeing an allocation of pages is done by chang-
ing their state in the page directory to MALLOC_FREE,
traversing the free-pages list to find the right place for
this run of pages, collapsing with either or both of the
two neighbouring entries if possible, and if above the
threshold: releasing some pages back to the kernel by
calling brk(2).
In a commit later that year, revision 1.7, December 11, 1995 (still before either book was published), mmap improvements were made to increase the odds of returning memory to the kernel. However, phkmalloc never used mmap to directly satisfy memory allocation requests. Instead, it used mmap to place the page directories (housekeeping data structures) outside the heap. From the commit message:
Quote:
Change phkmalloc so that the page directory is now floating and allocated
via mmap() up around the shared library area. Previously the directory
was allocated from space from it's own memory pool. Because of the way it
was being extended on processes with large malloced data segments (ie: inn)
once the page directory was extended for some reason, it was not possible
to lower the heap size any more to return pages to the OS.
Revision 1.93, January 13, 2006, of malloc.c replaced phkmalloc with jemalloc, whose malloc/free can use mmap/munmap. Among other benefits, this increases the opportunities to return memory to the kernel.
Notes:
1) In the malloc.c cvs history, we see that the initial version of FreeBSD malloc.c came from 4.4BSD Lite. Looking at the code confirms that revision of free() never returns memory to the system.
Last edited by alister; 10-27-2012 at 12:14 AM..
These 3 Users Gave Thanks to alister For This Post:
Hi Experts,
Our servers running Solaris 10 with SAP Application. The memory utilization always >90%, but the process on SAP is too less even nothing.
Why memory utilization on solaris always looks high?
I have statement about memory on solaris, is this true:
Memory in solaris is used for... (4 Replies)
Hello!
I've been struggling for not few hours with memory leaks on this
machine. I'm running linux 2.6.32-5-686, and the problem is as follows:
Some months ago, I have compiled kernel 2.6.33-2-686 without any issues
in this same machine. This week I have tried compiling GNUzilla Icecat
and... (23 Replies)
Hi all,
I have written a small code just to invoke main and return immediately. When built with libpthread on AIX box, valgrind throws lots of memory leak errors. But when built without libpthread, no issues at all.
Here is the sample run for your look. Any idea where I might be going wrong?... (3 Replies)
Hi,
Im working on Solaris 9 on SPARC-32 bit running on an Ultra-80, and I have to find out the following:-
1. Total Physical Memory in the system(total RAM).
2. Available Physical Memory(i.e. RAM Usage)
3. Total (Logical) Memory in the system
4. Available (Logical) Memory.
I know... (4 Replies)
Hi Unix lovers,
I am facing a strange problem about memory leak. One component of our product show memory leak at customer's end but not in development environment. The memory used by the exe goes on increasing at customer end but not in dev.
customer has same m/c(HP unix 11i) , the same... (1 Reply)
hi, i am a c++ programmer working on linux(redhat linux8.0) environment, i need to find out the memory leaks, so far i didn't used any tools, so what are the tools are available, and whic one is good to use. plz provide with a small example. (1 Reply)
Hi folks,
We are using following listed configurations for a particular application.
HP-UX 11i
Sun Java 2 SDK Standard Edition 1.4.1 (version shipped with WebLogic 8)
Oracle 9i Release 2 (Oracle 9.2.0)
BEA WebLogic Server 8.1 SP3
It seems a memory leak when we use above configurations.... (1 Reply)
Hi!! Experts,
Any ideas how to check for the memory leaks in a process during performance testing?? I dont use purify.. Any way of finding it out using default S/W in HP UX-11
Can U gimme pointers to site having good scripts/tutorials on performance testing??
Thanx in Advance..
:) (3 Replies)