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?
我是C的新手。我正在尝试使用malloc + free。我编写了以下测试但由于某种原因内存未完全释放(顶部仍然表示分配给处理的内存大约150MB)。这是为什么?
#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 个解决方案
#1
11
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.
top将告诉您分配给您的进程的物理内存量。虚拟内存是物理内存之上的抽象,而malloc / free则提供了抽象。
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.
malloc从程序堆中保留空间。堆只是程序的虚拟地址空间用于临时存储的区域。当你更多地调用malloc时,使用brk系统调用扩展堆。但是,尽管堆的虚拟大小增加,但在读取或写入新分配的内存之前,实际上并未分配物理内存。例如,由于您从不写入分配给记录内部字段的内存,因此这些分配不会占用任何物理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.
free只释放malloc分配的堆的部分。这不一定会减少堆的虚拟大小,因此可能无法释放与其关联的物理内存。这就是您没有看到物理内存使用量减少的原因。
#2
6
Unix memory management is lazy, it is not guaranteed to free process memory unless someone doesn't really need it. Here is good article.
Unix内存管理是懒惰的,除非有人真的不需要它,否则不能保证*进程内存。这是好文章。
Also I'd recommend you to check malloc() results, you definitely find at least some of them failed.
另外我建议你检查malloc()结果,你肯定发现它们中至少有一些失败了。
#3
3
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.
可能是因为您分配了10000000000000000字节(1000 * 10000 * 1000 * 1000 * 1000)= ~10000000000 Mbytes = 10000000 Gbytes的顺序,它会多次包装您的系统内存。
#1
11
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.
top将告诉您分配给您的进程的物理内存量。虚拟内存是物理内存之上的抽象,而malloc / free则提供了抽象。
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.
malloc从程序堆中保留空间。堆只是程序的虚拟地址空间用于临时存储的区域。当你更多地调用malloc时,使用brk系统调用扩展堆。但是,尽管堆的虚拟大小增加,但在读取或写入新分配的内存之前,实际上并未分配物理内存。例如,由于您从不写入分配给记录内部字段的内存,因此这些分配不会占用任何物理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.
free只释放malloc分配的堆的部分。这不一定会减少堆的虚拟大小,因此可能无法释放与其关联的物理内存。这就是您没有看到物理内存使用量减少的原因。
#2
6
Unix memory management is lazy, it is not guaranteed to free process memory unless someone doesn't really need it. Here is good article.
Unix内存管理是懒惰的,除非有人真的不需要它,否则不能保证*进程内存。这是好文章。
Also I'd recommend you to check malloc() results, you definitely find at least some of them failed.
另外我建议你检查malloc()结果,你肯定发现它们中至少有一些失败了。
#3
3
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.
可能是因为您分配了10000000000000000字节(1000 * 10000 * 1000 * 1000 * 1000)= ~10000000000 Mbytes = 10000000 Gbytes的顺序,它会多次包装您的系统内存。