Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insanity is free()

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


In this instance:

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 C did not return 984.


If I run the same procedure twice I get:

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

Looks fine.


But, in this instance:

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

Huh? Why is the memory not freeing here?

like image 211
Mark Richards Avatar asked Sep 22 '11 22:09

Mark Richards


People also ask

What is the real definition of insanity in the book Insanity?

Insanity: The Real Definition. "The definition of insanity is repeating the same mistakes over and over again and expecting different results," utters the know-it-all guy in the coffee shop offering free "therapy" to his visibly shaken friend.

What does it mean to be insane?

In modern times, labeling someone as insane often carries little or no medical meaning and is rather used as an insult or as a reaction to someone doing something crazy. The following quote defining insanity is often used: "The definition of insanity is doing the same thing over and over again and expecting a different result."

Is there a free server for insanity MMORPG?

Furthermore there’s a brand new rebalance, a revamped collector system and dozens of exceptionally fashion transmutes. Join Insanity mmorpg free server to discover exclusive dungeon and farming content, meet hundreds of players from all over the wor$ Do you like Insanity MMORPG for its extraordinary look?

What is an insanity defense?

An insanity defense is based on the theory that the majority of individuals can choose to follow the law or not. A few individuals cannot be held accountable because mental illness or defect deprives them of making a rational and voluntary choice.


2 Answers

The fact that a block of memory has been freed does not necessarily make that block the most eligible for a subsequent allocation. There are several strategies for a memory manager to select a block of memory (best fit, worst fit, first fit).

Most memory managers also attempt to coalesce free blocks, but some try to let the free blocks "age" as long as possible before coalescing, on the theory that as they age, there's a better chance that blocks next to them will also be freed, improving the success rate at coalescing blocks (thus reducing fragmentation).

The fact that the block wasn't used to satisfy your next allocation request does not mean it wasn't freed.

like image 198
Jerry Coffin Avatar answered Oct 02 '22 17:10

Jerry Coffin


From the free() man page: "Occasionally, free can actually return memory to the operating system and make the process smaller. Usually, all it can do is allow a later call to malloc to reuse the space. In the meantime, the space remains in your program as part of a free-list used internally by malloc."

like image 35
nw. Avatar answered Oct 02 '22 18:10

nw.