Linux 如果我使用 mmap 而不是 malloc 分配内存怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8870008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What if I allocate memory using mmap instead of malloc?
提问by MetallicPriest
What are the disadvantages of allocating memory using mmap
(with MAP_PRIVATE and MAP_ANONYMOUS) than using malloc
? For data in function scope, I would use stack memory anyway and therefore not malloc.
使用mmap
(使用 MAP_PRIVATE 和 MAP_ANONYMOUS)分配内存比使用 有malloc
什么缺点?对于函数范围内的数据,无论如何我都会使用堆栈内存,因此不会malloc.
One disadvantage that comes to mind is for dynamic data structures such as trees and linked lists, where you frequently require to allocate and deallocate small chunks of data. Using mmap
there would be expensive for two reasons, one for allocating at granularity of 4096 bytes and the other for requiring to make a system call.
想到的一个缺点是动态数据结构,例如树和链表,您经常需要分配和释放小块数据。使用mmap
那里会很昂贵,原因有两个,一个是以 4096 字节的粒度分配,另一个是需要进行系统调用。
But in other scenarios, do you think malloc
is better than mmap
? Secondly, am I overestimating disadvantage of mmap
for dynamic data structures?
但在其他场景中,你认为malloc
比mmap
? 其次,我是否高估mmap
了动态数据结构的缺点?
One advantage of mmap
over malloc
I can think of is that memory is immediately returned to the OS, when you do munmap
, whereas with malloc/free
, I guess memory uptil the data segment break point is never returned, but kept for reusage.
的一个优点mmap
超过malloc
我能想到的是,内存被立即返回到操作系统,当你这样做munmap
,而用malloc/free
的,我猜内存uptil数据段断点再也没有回来,但存放回用。
回答by Fred Foo
Yes, malloc
is better than mmap
. It's much easier to use, much more fine-grained and much more portable. In the end, it will call mmap
anyway.
是的,malloc
比mmap
。它更易于使用、更细粒度且更便携。最后,它mmap
无论如何都会调用。
If you start doing everyday memory management with mmap
, you'll want to implement some way of parceling it out in smaller chunks than pages and you will end up reimplementing malloc
-- in a suboptimal way, probably.
如果您开始使用 进行日常内存管理mmap
,您将需要实现某种方式,将它分成比页面更小的块,并且最终会重新实现malloc
——可能以次优的方式。
回答by Kerrek SB
First off, mmap()
is a platform specific construct, so if you plan on writing portable C, it's already out.
首先,mmap()
是特定于平台的构造,因此如果您打算编写可移植 C,它已经过时了。
Second, malloc()
is essentially implemented in terms of mmap()
, but it's a sort of intelligent library wrapper around the system call: it will request new memory from the system when needed, but until then it will pick a piece of memory in an area that's already committed to the process.
其次,malloc()
本质上是根据 实现的mmap()
,但它是一种围绕系统调用的智能库包装器:它会在需要时从系统请求新内存,但在那之前它会在已经提交的区域中选择一块内存过程。
Therefore, if you want to do ordinary dynamic memory allocation, use malloc()
, end of story. Use of mmap()
for memory allocation should be reserved for special situations (e.g. if you actually want a whole page for yourself, aligned at the page boundary), and always abstracted into a single piece of library code so that others may easily understand what you're doing.
因此,如果要进行普通的动态内存分配,请使用malloc()
, 故事结束。mmap()
应该为特殊情况保留 for 内存分配的使用(例如,如果您确实想要自己的整个页面,在页面边界对齐),并且始终抽象为单个库代码,以便其他人可以轻松理解您的内容正在做。
回答by Mark Lakata
One feature that mmap
has that malloc
doesn't, is mmap
allows you to allocate using Huge Pages (flag argument has MAP_HUGETLB
set), while malloc
doesn't have that option.
一个功能mmap
有malloc
没有,是mmap
让您在使用大内存页(标志参数必须分配MAP_HUGETLB
集),而malloc
没有这样的选择。