为什么 malloc() 可以互换调用 mmap() 和 brk()? [英] Why does malloc() call mmap() and brk() interchangeably?

查看:25
本文介绍了为什么 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.

所以brkmmap肯定有很大的区别,但是理论上我们应该可以使用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天全站免登陆