insanity is free ()

В моей простой программе на C (gnu linux) я получаю значение rss из proc / stat.
int GetRSS () возвращает значение RSS из proc / stat для моего процесса.


В этом случае:

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());
}

Я получаю:

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

Я не могу объяснить, почему C не вернул 984 .


Если я запускаю ту же процедуру дважды, я получаю:

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

Выглядит нормально.


Но в этом случае:

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());

Я получаю:

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

А? Почему здесь не освобождается память?

5
задан Mateen Ulhaq 22 September 2011 в 22:55
поделиться