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 hidden or 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]); | |
} |
With uncommented mallopt
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
With commented
mallopt
100th iteration:
900th iteration:
Valgrind output: