问题描述
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
size_t sz = atol(argv[1]);
char *arr = malloc(sz);
sleep(10);
}
我编译了这段代码并尝试运行它,并使用了 pmap
来查看程序的内存映射。
I compiled this code and tried to run it, used pmap
to see the memory mapping of the program.
当我使用诸如 1024000
这样的大数字时,会得到一个像这样的映射:
When I use some big number like 1024000
, I get a mapping like this:
3901: ./alloc_program 1024000
0000560192f43000 4K r---- alloc_program
0000560192f44000 4K r-x-- alloc_program
0000560192f45000 4K r---- alloc_program
0000560192f46000 4K r---- alloc_program
0000560192f47000 4K rw--- alloc_program
0000560192fac000 132K rw--- [ anon ]
00007f75b69e9000 1004K rw--- [ anon ] <---- I believe this is the allocated memory
00007f75b6ae4000 148K r---- libc-2.31.so
00007f75b6b09000 1504K r-x-- libc-2.31.so
00007f75b6c81000 296K r---- libc-2.31.so
00007f75b6ccb000 4K ----- libc-2.31.so
00007f75b6ccc000 12K r---- libc-2.31.so
00007f75b6ccf000 12K rw--- libc-2.31.so
00007f75b6cd2000 24K rw--- [ anon ]
00007f75b6ce7000 4K r---- ld-2.31.so
00007f75b6ce8000 140K r-x-- ld-2.31.so
00007f75b6d0b000 32K r---- ld-2.31.so
00007f75b6d14000 4K r---- ld-2.31.so
00007f75b6d15000 4K rw--- ld-2.31.so
00007f75b6d16000 4K rw--- [ anon ]
00007ffe2b26e000 132K rw--- [ stack ]
00007ffe2b318000 12K r---- [ anon ]
00007ffe2b31b000 4K r-x-- [ anon ]
ffffffffff600000 4K --x-- [ anon ]
total 3496K
我想标记的行是malloc分配的内存(也许我错了)。
但是当我使用诸如 10240
之类的小数字时,我看不到分配了任何东西:
I suppose that the marked line is the memory allocated by malloc (maybe I'm wrong).But when I use some small number like 10240
, I don't see that anything is allocated:
3879: ./alloc_program 10240
000055e428e26000 4K r---- alloc_program
000055e428e27000 4K r-x-- alloc_program
000055e428e28000 4K r---- alloc_program
000055e428e29000 4K r---- alloc_program
000055e428e2a000 4K rw--- alloc_program
000055e42a257000 132K rw--- [ anon ]
00007f102332c000 148K r---- libc-2.31.so
00007f1023351000 1504K r-x-- libc-2.31.so
00007f10234c9000 296K r---- libc-2.31.so
00007f1023513000 4K ----- libc-2.31.so
00007f1023514000 12K r---- libc-2.31.so
00007f1023517000 12K rw--- libc-2.31.so
00007f102351a000 24K rw--- [ anon ]
00007f102352f000 4K r---- ld-2.31.so
00007f1023530000 140K r-x-- ld-2.31.so
00007f1023553000 32K r---- ld-2.31.so
00007f102355c000 4K r---- ld-2.31.so
00007f102355d000 4K rw--- ld-2.31.so
00007f102355e000 4K rw--- [ anon ]
00007fff1d513000 132K rw--- [ stack ]
00007fff1d570000 12K r---- [ anon ]
00007fff1d573000 4K r-x-- [ anon ]
ffffffffff600000 4K --x-- [ anon ]
total 2492K
1-为什么在内存大小相对较小时不分配?
1 - Why it doesn't allocate when the memory size is relatively small?
2-为什么分配的内存大小不完全相同?在第一次运行中,它显示的大小为 1004KB
,而我只分配了 1000KB
。
2 - Why the allocated memory size is not exactly the same? In the first run, it shows that the size is 1004KB
while I've only allocated 1000KB
.
推荐答案
函数 malloc
的任务是在需要时为应用程序提供内存。从理论上讲, malloc
可以按照您的建议将所有内存分配请求转发到操作系统的,因此它仅充当内核内存分配器的包装器。但是,这样做有以下缺点:
The task of the function malloc
is to provide the application with memory, whenever it asks for it. Theoretically, malloc
could, as you suggest, just forward all requests for memory allocations to the operating system's kernel, so that it only acts as a wrapper for the kernel's memory allocator. However, this has the following disadvantages:
- 内核一次只能提供大量内存,至少一个,根据操作系统的配置,通常至少为4096个字节。因此,如果应用程序仅要求10个字节的内存,则会浪费大量内存。
- 就CPU性能而言是昂贵的。
- The kernel only provides large amounts of memory at once, at least one page of memory, which is, depending on the configuration of the operating system, normally at least 4096 bytes. Therefore, if an application asked for only 10 bytes of memory, a lot of memory would be wasted.
- System calls are expensive in terms of CPU performance.
由于这些原因,对于 malloc
不会将内存分配请求直接转发到内核,而是充当应用程序的内存分配请求和内核之间的中介。它从内核请求更多的内存,因此它可以满足来自应用程序的许多较小的内存分配请求。
For these reasons, it is more efficient for malloc
to not forward memory allocation requests directly to the kernel, but to rather act as an intermediary between the application's memory allocation requests and the kernel. It requests memory in larger amounts from the kernel, so that it can satisfy many smaller memory allocation requests from the application.
因此,只有一次请求大量内存时,它才会 malloc
将内存分配请求转发给内核。
Therefore, only when asking for a large amount of memory at once, will malloc
forward that memory allocation request to the kernel.
malloc
分配器必须跟踪分配给应用程序的所有内存分配,还必须跟踪所有分配给应用程序的内存分配。内核已授予它的内存分配。要存储此信息,它需要一点额外的存储空间。此额外空间称为开销。
The malloc
allocator must keep track of all the memory allocations it granted to the application and also keep track of all the memory allocations that it has been granted by the kernel. To store this information, it requires a bit of addititional memory space. This additional space is called "overhead".
这篇关于为什么malloc在达到一定阈值之前不分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!