Intricacies of malloc and free
I have two related questions hence I am asking them in this single thread.
Q1) How can I confirm if my OS is clearing un-"free"'ed memory (allocated using malloc) automatically when a program terminates? I am using Ubuntu 11.04, 32-bit with gcc-4.5.2
As per a tutorial page by Steven Summit here, "Freeing unused memory (malloc'ed) is a good idea, but it's not mandatory. When your program exits, any memory which it has allocated but not freed should be automatically released. If your computer were to somehow ``lose'' memory just because your program forgot to free it, that would indicate a problem or deficiency in your operating system ."
Q2) Suppose, foo.c mallocs a B-bytes memory. Later on, foo.c frees this B-bytes memory locations and returns it to the OS. Now my question is , can those PARTICULAR B-bytes of memory locations be re-allocated to foo.c (by the OS) in the current instance OR those B-bytes can't be allocated to foo.c untill its current instance terminates ?
EDIT : I would recommend everyone who reads my question to read the answer to a similar question here and here. Both answers explain the interaction and working of malloc() and free() in good detail without using very esoteric terms. To understand the DIFFERENCE between memory-management tools used by kernel (eg brk(), mmap()) and those used by the C-compiler (eg malloc(), free()), this is a MUST READ.
When a process ends either thru a terminating signal, eg SIGSEGV
, or thru the _exit(2) system call (which happens to be called also when returning from main
), all the process resources are released by the kernel. In particular, the process address space, including heap memory (allocated with mmap(2) (or perhaps sbrk(2)
) syscall (used by malloc
library function) is released.
Of course, the free
library function either (often) makes the freed memory zone reusable by further future calls to malloc
or (occasionally, for large memory zones) release some big memory chunk to the kernel using eg munmap(2)
system call.
To know more about the memory map of process 1234, read sequentially the /proc/1234/maps
pseudo-file (or /proc/self/maps
from inside the process). The /proc file system is the preferred way to query the kernel about processes. (there is also /proc/self/statm
and /proc/self/smaps
and many other interesting things).
The detailed behavior of free
and malloc
is implementation dependent. You should view malloc
as a way to get heap memory, and free
as a way to say that a previously malloc
-ed zone is useless, and the system (ie standard C library + kernel) can do whatever it wants with it.
Use valgrind to hunt memory leak bugs. You could also consider using Boehm's conservative garbage collector, ie use GC_malloc
instead of malloc
and don't bother about freeing manually memory.
Most Modern OS will reclaim the allocated memory so you need not worry about that.
The OS doesn't understand if your application/program leaked memory or not it simply reclaims what it allocated to an process once the process completes.
Yes freed memory can be reused(if needed) & the reuse can happen in the same instantiation.
Q1. You just have to assume that the operating system is behaving correctly.
Q2. There is no reason why the bytes can't be reallocated to foo.c it just depends on how the memory allocation routines work.
链接地址: http://www.djcxy.com/p/80402.html上一篇: 了解malloc和指针增量与免费
下一篇: 错综复杂的malloc和免费