This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/10756] free doesn't free all memory alloced by memalign


------- Additional Comments From a_llison_life at yahoo dot com  2009-10-12 01:15 -------
http://www.gnu.org/software/libc/manual/html_node/Freeing-after-Malloc.html#Freeing-after-Malloc

The description of free says "Occasionally, free can actually return memory to
the operating system and make the process smaller. Usually, all it can do is
allow a later call to malloc to reuse the space. In the meantime, the space
remains in your program as part of a free-list used internally by malloc."

So, your test program is not proving a bug, it is just showing that in this
case, not all the space allocated by memalign's call to malloc was released from
the process. 

Try running this enhanced test program to show the difference:
#include <malloc.h>
#include <stdio.h>

static void printMemUsage()
{
 struct mallinfo mi;
 mi = mallinfo();
 printf("MemUsage Arena: %d\n", mi.arena);
 printf("MemUsage Fordblks: %d\n", mi.fordblks);
 printf("=============================\n");
}

int main(int argc, char **argv)
{
 printf("Memory Usage at program start\n");
 printMemUsage();
 void * dyn_mem = memalign(16, 65535);
 printf("Memory Usage after memalign\n");
 printMemUsage();
 free(dyn_mem);
 printf("Memory Usage after free of memory allocated by memalign\n");
 printMemUsage();
 void * more_mem = malloc(168);
 printf("Memory Usage after another malloc\n");
 printMemUsage();
 free(more_mem);
 printf("Memory Usage after free of the memory allocated by the second
allocation\n");
 printMemUsage();
 return 0;
}

The fordblks member of the mallinfo struct describes how much free heap space is
maintained by malloc on its free list
(http://www.gnu.org/s/libc/manual/html_node/Statistics-of-Malloc.html)

So, that extra memory you are seeing is not a memory leak.  The next time you
request memory with malloc, that memory can be used.

This output demonstrates this behavior:

Memory Usage at program start
MemUsage Arena: 0
MemUsage Fordblks: 0
=============================
Memory Usage after memalign
MemUsage Arena: 200704
MemUsage Fordblks: 135144
=============================
Memory Usage after free of memory allocated by memalign
MemUsage Arena: 135168
MemUsage Fordblks: 135168
=============================
Memory Usage after another malloc
MemUsage Arena: 135168
MemUsage Fordblks: 134992
=============================
Memory Usage after free of the memory allocated by the second allocation
MemUsage Arena: 135168
MemUsage Fordblks: 135168
=============================


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


http://sourceware.org/bugzilla/show_bug.cgi?id=10756

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]