Madness is free ()

In my simple C program (gnu linux), I get the rss value from proc / stat.
int GetRSS()returns the RSS value from proc / stat for my process.


In this case:

printf("A RSS=%i\n", GetRSS());
char *cStr = null;
cStr = malloc(999999);
if (cStr != NULL)
{
    printf("B RSS=%i\n", GetRSS());
    free(cStr);
    printf("C RSS=%i\n", GetRSS());
}

I get:

A RSS=980
B RSS=984
C RSS=980

I can’t explain why I Cdidn’t return it 984.


If I perform the same procedure twice, I get:

A RSS=980
B RSS=984
C RSS=980
B RSS=984
C RSS=980

Looks nice.


But in this case:

struct _test
{
    char *pChar;
}
struct _test **test_ptr;

int i = 0;
printf("D RSS=%i\n",GetRSS());
assert(test_ptr = (struct _test **)malloc( (10000) * sizeof(struct _test *)));

for (i = 0; i < 1000; i++)
{
    assert(test_ptr[i] = (struct _test *)malloc(sizeof(struct _test)));
    test_ptr[i]->pChar=strdup("Some garbage");
}

printf("E RSS=%i\n", GetRSS());

for (i=0; i<1000; i++)
{
    free(test_ptr[i]->pChar);
    free(test_ptr[i]);
}

free(test_ptr);
printf("F RSS=%i\n", GetRSS());

I get:

D RSS=980
E RSS=1024
F RSS=1024
D RSS=1024
E RSS=1024
F RSS=1024

AND? Why is memory not freed here?

+5
source share
3 answers

, , . , ( , , ).

, "" , , , , , ( ).

, , , .

+5

free(): " . , , malloc ., , malloc."

+2

malloc . ( ), ( , ).

, . . .

+1

All Articles