Skip to content

Instantly share code, notes, and snippets.

@mshabunin
Last active September 19, 2019 12:06
Show Gist options
  • Save mshabunin/048b97c7293b6d89d030554fa00d2a61 to your computer and use it in GitHub Desktop.
Save mshabunin/048b97c7293b6d89d030554fa00d2a61 to your computer and use it in GitHub Desktop.
Reproduce issue with posix_memalign
// command: g++ posix_memalign_not_reclaiming.cpp -o test
#include <malloc.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
const size_t ALIGN = 64;
const size_t ITERATIONS = 1000;
const size_t SIZE = 3 * 1000 * 1000;
inline void delay() { usleep(10000); }
inline void * allocate0(size_t size)
{
void * ptr;
posix_memalign(&ptr, ALIGN, SIZE);
memset(ptr, 0, size);
return ptr;
}
int main()
{
// Uncomment to fix the issue
// mallopt(M_TRIM_THRESHOLD, 128*1024);
void * bufs[ITERATIONS];
void * buf = allocate0(SIZE);
for (size_t i = 0; i < ITERATIONS; ++i)
{
void * buf2 = allocate0(SIZE);
free(buf);
buf = buf2;
bufs[i] = malloc(30);
delay();
// Uncomment to print statistics
// if (i % 100 == 0) malloc_stats();
}
free(buf);
for (size_t i = 0; i < ITERATIONS; ++i)
free(bufs[i]);
}
@mshabunin
Copy link
Author

With commented mallopt

posix_memalign
100th iteration:

system bytes     =     135168
in use bytes     =        640
Total (incl. mmap):     
system bytes     =    3137536
in use bytes     =    3003008
max mmap regions =          2
max mmap bytes   =    6004736

900th iteration:

system bytes     = 2700193792
in use bytes     =    3044304
Total (incl. mmap):     
system bytes     = 2700193792
in use bytes     =    3044304
max mmap regions =          2
max mmap bytes   =    6004736

Valgrind output:

==23898== HEAP SUMMARY:
==23898==     in use at exit: 0 bytes in 0 blocks
==23898==   total heap usage: 2,001 allocs, 2,001 frees, 3,003,030,000 bytes allocated

@mshabunin
Copy link
Author

With uncommented mallopt

posix_memalign_trim

100th iteration:

system bytes     =     135168
in use bytes     =        640
Total (incl. mmap):     
system bytes     =    3137536
in use bytes     =    3003008
max mmap regions =          2
max mmap bytes   =    6004736

900th iteration:

system bytes     =     135168
in use bytes     =      43840
Total (incl. mmap):
system bytes     =    3137536
in use bytes     =    3046208
max mmap regions =          2
max mmap bytes   =    6004736

Valgrind output:

==25831== HEAP SUMMARY:
==25831==     in use at exit: 0 bytes in 0 blocks
==25831==   total heap usage: 2,001 allocs, 2,001 frees, 3,003,030,000 bytes allocated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment