Last active
September 19, 2019 12:06
-
-
Save mshabunin/048b97c7293b6d89d030554fa00d2a61 to your computer and use it in GitHub Desktop.
Reproduce issue with posix_memalign
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With uncommented
mallopt
100th iteration:
900th iteration:
Valgrind output: