@ Anton Lakotko , maybe this testik will help you with something.
#include <stdio.h> #include <stdlib.h> #include <malloc.h> int pri_mallinfo (struct mallinfo *mi) { printf ("arena\t\t%d non-mmapped space allocated from system\n" "ordblks\t\t%d number of free chunks\n" "smblks\t\t%d number of fastbin blocks\n" "hblks\t\t%d number of mmapped regions\n" "hblkhd\t\t%d space in mmapped regions\n" "usmblks\t\t%d maximum total allocated space\n" "fsmblks\t\t%d space available in freed fastbin blocks\n" "uordblks\t%d total allocated space\n" "fordblks\t%d total free space\n" "keepcost\t%d top-most, releasable (via malloc_trim) space\n", mi->arena, mi->ordblks, mi->smblks, mi->hblks, mi->hblkhd, mi->usmblks, mi->fsmblks, mi->uordblks, mi->fordblks, mi->keepcost); } main (int ac, char *av[]) { puts ("Hi"); struct mallinfo mi = mallinfo(); pri_mallinfo(&mi); int i, s = 0; char *p[1000]; for (i = 1; i < 1001; i++) { p[i-1] = malloc(i*i); s += i*i; } printf ("\nmalloc %d times total %d bytes\n",i-1,s); mi = mallinfo(); pri_mallinfo(&mi); for (i = 0; i < 1000; i++) free(p[i]); puts ("\nfree all"); mi = mallinfo(); pri_mallinfo(&mi); printf ("\nreturn half non-mmaped via malloc_trim(%d) to system\n", mi.keepcost); malloc_trim(mi.keepcost/2); mi = mallinfo(); pri_mallinfo(&mi); exit (puts("Bye") == EOF); }
We broadcast and run
avp@avp-ubu1:~/hashcode$ gcc mal.c avp@avp-ubu1:~/hashcode$ ./a.out Hi arena 0 non-mmapped space allocated from system ordblks 1 number of free chunks smblks 0 number of fastbin blocks hblks 0 number of mmapped regions hblkhd 0 space in mmapped regions usmblks 0 maximum total allocated space fsmblks 0 space available in freed fastbin blocks uordblks 0 total allocated space fordblks 0 total free space keepcost 0 top-most, releasable (via malloc_trim) space malloc 1000 times total 333833500 bytes arena 16015360 non-mmapped space allocated from system ordblks 1 number of free chunks smblks 0 number of fastbin blocks hblks 638 number of mmapped regions hblkhd 319283200 space in mmapped regions usmblks 0 maximum total allocated space fsmblks 0 space available in freed fastbin blocks uordblks 15884224 total allocated space fordblks 131136 total free space keepcost 131136 top-most, releasable (via malloc_trim) space free all arena 135168 non-mmapped space allocated from system ordblks 1 number of free chunks smblks 0 number of fastbin blocks hblks 0 number of mmapped regions hblkhd 0 space in mmapped regions usmblks 0 maximum total allocated space fsmblks 0 space available in freed fastbin blocks uordblks 0 total allocated space fordblks 135168 total free space keepcost 135168 top-most, releasable (via malloc_trim) space return half non-mmaped via malloc_trim(135168) to system arena 69632 non-mmapped space allocated from system ordblks 1 number of free chunks smblks 0 number of fastbin blocks hblks 0 number of mmapped regions hblkhd 0 space in mmapped regions usmblks 0 maximum total allocated space fsmblks 0 space available in freed fastbin blocks uordblks 0 total allocated space fordblks 69632 total free space keepcost 69632 top-most, releasable (via malloc_trim) space Bye avp@avp-ubu1:~/hashcode$
I admit, in my comment (that in * nix memory is not returned to the system) was wrong. As you can see part of the dynamically allocated memory is automatically returned after free (), a part can be returned forcibly (via malloc_trim).
grep test | grep -v grep
grep test | grep -v grep
:) - VladD