为什么malloc()交替调用mmap()和brk()? [英] Why does malloc() call mmap() and brk() interchangeably?

查看:240
本文介绍了为什么malloc()交替调用mmap()和brk()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C和堆内存的新手,仍然在努力理解动态内存分配。

I'm new to C and heap memory, still struggling to understand dynamic memory allocation.

我跟踪了Linux系统调用,发现如果我使用 malloc 请求少量的堆内存,然后 malloc 内部调用 brk

I traced Linux system calls and found that if I use malloc to request a small amount of heap memory, then malloc calls brk internally.

但是如果我使用 malloc 请求大量的堆内存,那么 malloc 内部调用 mmap

But if I use malloc to request a very large amount of heap memory, then malloc calls mmap internally.

因此, brk mmap ,但理论上我们应该能够使用 brk 分配堆内存,而不管请求的大小如何。那么为什么在分配大量内存时 malloc 调用 mmap

So there must be a big difference between brk and mmap, but theoretically we should be able to use brk to allocate heap memory regardless of the requested size. So why does malloc call mmap when allocating a large amount of memory?

推荐答案

那么为什么在分配大容量内存时malloc调用mmap?

简短的答案是提高效率在较新的Linux实现中以及随之而来的更新的内存分配算法。但是请记住,这是一个非常依赖于实现的主题,并且对于所讨论的特定Linux操作系统的不同年份和风格,其理由为何会有很大差异。

The short answer is for improved efficiency on newer implementations of Linux, and the updated memory allocation algorithms that come with them. But keep in mind that this is a very implementation dependent topic, and the whys and wherefores would vary greatly for differing vintages and flavors of the specific Linux OS being discussed.

这是关于 > mmap() brk()在Linux内存分配中起作用。而且,不是最近,但仍然很相关的 Linux Journal 文章,其中包含一些非常重要的内容

Here is fairly recent write-up regarding the low-level parts mmap() and brk() play in Linux memory allocation. And, a not so recent, but still relevant Linux Journal article that includes some content that is very on-point for the topic here, including this:


对于非常大的请求,malloc()使用mmap()系统调用查找
可寻址的内存空间。 当释放大块内存
但被位于
和它们之间的较小的,最近分配的块锁定时,此过程有助于减少内存碎片的负面
影响
分配空间的末尾。实际上,在这种情况下,如果使用brk()分配了
块,则即使进程释放了它,系统也将仍然无法使用


(强调我的)

For very large requests, malloc() uses the mmap() system call to find addressable memory space. This process helps reduce the negative effects of memory fragmentation when large blocks of memory are freed but locked by smaller, more recently allocated blocks lying between them and the end of the allocated space. In this case, in fact, had the block been allocated with brk(), it would have remained unusable by the system even if the process freed it.
(emphasis mine)

关于 brk()

偶然地, ... mmap()在早期的Unix版本中不存在。 brk()是当时增加进程数据段大小的唯一方法。带有mmap()的Unix的第一个版本是80年代中期的SunOS,第一个开源版本是1990年的BSD-Reno。。从那时起,内存分配算法的现代实现已进行了很多改进,从而大大降低了使用 brk()的必要性。

Regarding brk():
incidentally, "...mmap() didn't exist in the early versions of Unix. brk() was the only way to increase the size of the data segment of the process at that time. The first version of Unix with mmap() was SunOS in the mid 80's, the first open-source version was BSD-Reno in 1990.". Since that time, modern implementation of memory allocation algorithms have been refactored with many improvements, greatly reducing the need for them to include using brk().

这篇关于为什么malloc()交替调用mmap()和brk()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆