So it seems tose 400MB that are freed are still kept by the OSX, which is IMHO pretty lame behavior. I just wonder it there's any kernel function to trigger VM flush/cleanup. Alternative solution I can see is writing own memory pool that uses vm_allocate. Is this feasible Allan?
BTW. It is all about default OSX/libc allocator which keeps in the memory all blocks < 16KB, so they are not reclaimed at all (given back to the system).
But there's quite easy solution, alternative allocator ptmalloc (used by Linux glibc): http://www.malloc.de/en/index.html
I've tested it with simple program:
#include "stdio.h" #include "stdlib.h" #include "string.h"
int main(int argc, char const *argv[]) { char b[4*1024], c = 0; void *p[4*10*1024]; size_t i, len = sizeof(b)/sizeof(b[0]); printf("Preparing buffer...\n"); for(i = 0; i < sizeof(b)/sizeof(b[0]); ++i) { b[i] = c + 'a'; c = (c + 1) % 30; }
printf("Allocating...\n"); for(i = 0; i < sizeof(p)/sizeof(p[0]); ++i) { p[i] = malloc(len); memcpy(p[i], b, len); } printf("Press any key...\n"); getchar();
printf("Freing...\n"); for(i = 0; i < sizeof(p)/sizeof(p[0]); ++i) free(p[i]); printf("Press any key...\n"); getchar();
return 0; }
Without using libptmalloc3.a after freeing, it still uses 161 MB real memory with MALLOC_SMALL blocks. With libptmalloc3.a after freeing, memory drops to 512KB! So this may be the solution for TextMate to give back small allocated memory blocks back to system.
Cheers,