This test code demonstrates the problem:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#define ALLOC_MAX 100 * 1024 *1024
#define ALLOC_STEP 64
int main(void)
{
int i = ALLOC_STEP;
void *memptr = NULL;
void *temp = NULL;
memptr = malloc(i);
while(i < ALLOC_MAX) {
i += ALLOC_STEP;
temp = realloc(memptr, i);
if(temp != NULL) memptr = temp;
}
printf("freeing %ld bytes\n", i);
free(memptr);
return 0;
}
When built against newlib, running the above program will quickly use up all memory (ie. long before the program completes and far more memory than ultimately requested)
When built against clib2 it works correctly (but takes forever).
If I change it to allocate only 10MB, around 400MB of RAM disappears. I appreciate this might be an intentional design decision to speed up further realloc()s, but the amount that gets taken from the free pool seems completely disproportionate to the amounts being requested.
I hate to think how much software this is affecting.