tags:

views:

315

answers:

3
+2  Q: 

malloc and free

I am new to C I am trying to get comfortable with malloc + free. I have coded following test but for some reason the memory isn't freed completely (top still indicates about 150MB of memory allocated to process). Why is that?

#include <stdio.h>
#include <malloc.h>

typedef struct {
    char *inner;
} structure;

int main()
{
    int i;
    structure** structureArray;

    structureArray = (structure**)malloc(sizeof(structure*)*1000*10000);
    for (i = 0; i < 1000*10000;i++)
    {
     structureArray[i] = (structure*) malloc(sizeof(structure));
     structureArray[i]->inner = (char*) malloc(sizeof(char)*1000*1000*1000);
    }


    printf("freeing memory");

    for (i = 0; i < 1000*10000;i++)
    {
     free(structureArray[i]->inner);
     free(structureArray[i]);
    }
    free(structureArray);

    system("sleep 100");
    return 0;
}

coresponding Makefile:

all: test.c
    gcc -o test test.c
    ./test &
    top -p `pidof ./test`
    killall ./test
+3  A: 

Probably something due to you allocating of the order of 10000000000000000 bytes (1000*10000*1000*1000*1000) =~ 10000000000 Mbytes = 10000000 Gbytes which wraps your system memory multiple times.

dajobe
Not if the OP is on a 64-bit system.
Jay Conrod
On 64-bit Linux, you get away with it despite allocating so much memory because it uses a lazy allocator. Pretty much anywhere else, you'd be out of (virtual) memory long ago. And on Linux, you'd be out of memory. I want to up-vote you, but you need to indicate that it is not quite as simple as 'wrapping system memory multiple times'.
Jonathan Leffler
System memory meaning physical memory as opposed to virtual memory. It seems unlikely the questioner has a system with 10000000 Gbytes of physical memory thus the program is probably not what is intended.
dajobe
+7  A: 

Unix memory management is lazy, it is not guaranteed to free process memory unless someone doesn't really need it. Here is good article.

Also I'd recommend you to check malloc() results, you definitely find at least some of them failed.

Roman Nikitchenko
The failure of the malloc will be the fact the the OS just won't let your process allocate anymore memory.
Evan
+7  A: 

top will tell you the amount of physical memory assigned to your process. Virtual memory is an abstraction on top of physical memory, and malloc/free provide an abstraction on top of that.

malloc reserves space from the heap of your program. The heap is simply an area your program's virtual address space used for temporary storage. As you call malloc more, the heap is expanded using the brk system call. However, although the virtual size of your heap increases, physical memory isn't actually assigned until you read or write to your newly allocated memory. For instance, since you never write to the memory allocated to the inner fields of your records, those allocations will not take up any physical RAM.

free just releases parts of the heap allocated by malloc. This doesn't necessarily reduce the virtual size of the heap, so the physical memory associated with it may not be released. This is why you're not seeing a reduction in physical memory usage.

Jay Conrod
+1. `free` keeping memory around is an important point.
Michael E
Thank you, this explanation is very throughout.
Kalaz