This question is a bit long due to the source code, which I tried to simplify as much as possible. Please carry me and thank you for reading.
I have an application with a loop that runs potentially millions of times. Instead of several thousand to millions of calls malloc/ freein this cycle, I would like to make one mallocin front, and then from several thousand to millions of calls realloc.
But I ran into a problem when my application consumes several GB of memory and kills itself when I use realloc. If I use malloc, my memory usage is perfect.
If I run smaller test datasets with valgrindmemtest, it does not report memory leaks with mallocor realloc.
I have verified that I map each malloc-ed (and then realloc-ed) object to the corresponding one free.
So, theoretically, I am not a memory leak, just using reallocit seems to absorb all my available RAM, and I would like to know why and what I can do to fix it.
Initially, I have something like this that uses mallocand works correctly:
Malloc Code
void A () {
do {
B();
} while (someConditionThatIsTrueForMillionInstances);
}
void B () {
char *firstString = NULL;
char *secondString = NULL;
char *someOtherString;
C((const char *)someOtherString, &firstString, &secondString);
fprintf(stderr, "first: [%s] | second: [%s]\n", firstString, secondString);
if (firstString)
free(firstString);
if (secondString)
free(secondString);
}
void C (const char *someOtherString, char **firstString, char **secondString) {
char firstBuffer[BUFLENGTH];
char secondBuffer[BUFLENGTH];
*firstString = malloc(strlen(firstBuffer)+1);
strncpy(*firstString, firstBuffer, strlen(firstBuffer)+1);
*secondString = malloc(strlen(secondBuffer)+1);
strncpy(*secondString, secondBuffer, strlen(secondBuffer)+1);
}
It works great. But I want something faster.
Now I am testing the layout reallocthat malloc-s only once:
Realloc Code
void A () {
char *firstString = NULL;
char *secondString = NULL;
do {
B(&firstString, &secondString);
} while (someConditionThatIsTrueForMillionInstances);
if (firstString)
free(firstString);
if (secondString)
free(secondString);
}
void B (char **firstString, char **secondString) {
char *someOtherString;
C((const char *)someOtherString, &(*firstString), &(*secondString));
fprintf(stderr, "first: [%s] | second: [%s]\n", *firstString, *secondString);
}
void C (const char *someOtherString, char **firstString, char **secondString) {
char firstBuffer[BUFLENGTH];
char secondBuffer[BUFLENGTH];
*firstString = realloc(*firstString, strlen(firstBuffer)+1);
strncpy(*firstString, firstBuffer, strlen(firstBuffer)+1);
*secondString = realloc(*secondString, strlen(secondBuffer)+1);
strncpy(*secondString, secondBuffer, strlen(secondBuffer)+1);
}
If I look at the output free -mon the command line, when I run this test based on realloca large dataset that causes the millionth cycle condition, my memory goes from 4 GB to 0 and the application crashes.
realloc, ? , .