Why does realloc eat tons of memory?

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;

    /* populate someOtherString with data from stream, for example */

    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];

    /* populate buffers with some data from tokenizing someOtherString in a special way */

    *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;

    /* populate someOtherString with data from stream, for example */

    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];

    /* populate buffers with some data from tokenizing someOtherString in a special way */

    /* realloc should act as malloc on first pass through */

    *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, ? , .

+3
3

realloc , . A malloc/free , a realloc, .

realloc , malloc/free. realloc s. I.e., :

malloc(A);
malloc(B);

while (...)
{
    malloc(A_temp);
    free(A);
    A= A_temp;
    malloc(B_temp);
    free(B);
    B= B_temp;
}

:

while (...)
{
    malloc(A);
    malloc(B);
    free(A);
    free(B);
}

, ; , .

+8

realloc, , - . , , . , , , , .

. , .. , , - , (, realloc, ), , , .

+1

, &(*firstString) , firstString, , A. , , , NULL, , . , , A - .

EDIT: Well, this is an amazing theory, but I seem to be mistaken in the compilers that are available to me for testing.

0
source

Source: https://habr.com/ru/post/1775077/


All Articles