Memory Leaks


 
Thread Tools Search this Thread
Top Forums Programming Memory Leaks
# 1  
Old 10-25-2012
Memory Leaks

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.

Code:
main()
{
    malloc(1gb)

    return(0)
}

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
# 2  
Old 10-25-2012
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:
# 3  
Old 10-25-2012
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.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 10-25-2012
Quote:
Originally Posted by Corona688
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

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 10-25-2012
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.
# 6  
Old 10-26-2012
Quote:
Originally Posted by alister
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.
# 7  
Old 10-26-2012
Quote:
Originally Posted by shamrock
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:
Code:
1.3     ! phk       927:     }
        !           928:     if (!pf->next &&
        !           929:       pf->size > malloc_cache &&
        !           930:       pf->end == malloc_brk &&
        !           931:       malloc_brk == sbrk(0)) {
        !           932:        pf->end = pf->page + malloc_cache;
        !           933:        pf->size = malloc_cache;
        !           934:        TRACE(("%6d U %p %d\n",malloc_event++,pf->end,pf->end - pf->page));
        !           935:        brk(pf->end);    <-- memory returned here
        !           936:        malloc_brk = pf->end;


From phkmalloc's author himself in Malloc(3) revisited:
Quote:
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.

For your convenience: FreeBSD malloc.c cvs history

Regards,
Alister


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:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

[DOUBT] Memory high in idle process on Solaris 10 (Memory Utilization > 90%)

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)
Discussion started by: edydsuranta
4 Replies

2. Emergency UNIX and Linux Support

Memory leaks on compilations

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)
Discussion started by: teresaejunior
23 Replies

3. AIX

valgrind - pthread memory leaks on AIX

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)
Discussion started by: visionofarun
3 Replies

4. Solaris

How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9

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)
Discussion started by: 0ktalmagik
4 Replies

5. UNIX and Linux Applications

Looking for memory leaks freeware tools

Hello all Is there good free ware tools to check software memory leaks ? Some thing like purify on unix platforms sun/hp/linux Thanks (3 Replies)
Discussion started by: umen
3 Replies

6. Shell Programming and Scripting

Find memory leaks shell script ???

Hi all, Has anyone out there a shell script to detect memory leaks on unix machines? And if so what way did they go about it .? (5 Replies)
Discussion started by: nano2
5 Replies

7. UNIX for Advanced & Expert Users

strange problem with memory leaks

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)
Discussion started by: shriashishpatil
1 Replies

8. Programming

Tool for finding memory leaks

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)
Discussion started by: sarwan
1 Replies

9. HP-UX

Memory leaks on HP-UX 11i

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)
Discussion started by: gimhan90
1 Replies

10. UNIX for Advanced & Expert Users

memory leaks

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)
Discussion started by: jyotipg
3 Replies
Login or Register to Ask a Question