使用map_private vs map_shared时,mmap'
我在任何地方都没有明确看过这个,所以我只是想澄清一下。这一切都是在单个螺纹程序的上下文中:说使用mmap
,使用map_private
选项时,我们有一个10GB文本文件。最初,我应该期望看到使用0GB居民内存。现在说我修改文件中的每个字符。然后,这需要在居民内存中需要10GB吗?如果不是,为什么不呢?
现在,如果我们做了同一件事,但是使用map_shared
,我应该期望居民的内存使用情况如何?
I haven't seen this explicitly anywhere so I just wanted to clarify. This is all in the context of a single threaded program: say we have a 10GB text file when we open with mmap
, using the MAP_PRIVATE
option. Initially of course, I should expect to see 0GB resident memory used. Now say I modify every character in the file. Will this then require 10GB in resident memory? And if not, why not?
Now what if we did the same thing but with MAP_SHARED
, what should I expect the resident memory usage to look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
map_shared
创建由原始文件支持的映射。数据的任何更改都写回该文件(假设读/写映射)。map_private
创建由原始文件支持的映射,仅读取。如果您在映射中更改字节,则OS会创建一个新的页面,该页面占据了物理内存,并由交换(如果有)支持。对居民设定大小的影响不取决于映射类型:如果它们积极访问(读取或写入),则页面将在您的居民集中。如果操作系统需要物理内存,则删除未积极访问的页面(如果清洁),或写入原始文件或交换(如果脏),则取决于映射类型)。
这两种类型的不同,总体承诺与物理记忆和交换。共享的映射不会增加此承诺,私人映射确实可以。如果您没有足够的组合内存和交换来保存私有映射的每个页面,并且您写入每个页面,那么您(或可能其他一些过程)将被遗忘的守护程序杀死。
更新:我上面写的内容适用于内存映射文件。您可以用
map_shared
映射一个匿名块(map_anonymous
),在这种情况下,内存是由交换而不是文件来支持的。MAP_SHARED
creates a mapping that is backed by the original file. Any changes to the data are written back to that file (assuming a read/write mapping).MAP_PRIVATE
creates a mapping that is backed by the original file for reads only. If you change bytes in the mapping, then the OS creates a new page that is occupies physical memory and is backed by swap (if any).The impact on resident set size is not dependent on the mapping type: pages will be in your resident set if they're actively accessed (read or write). If the OS needs physical memory, then pages that are not actively accessed are dropped (if clean), or written to either the original file or swap (if dirty, and depending on mapping type).
Where the two types differ is in total commitment against physical memory and swap. A shared mapping doesn't increase this commitment, a private mapping does. If you don't have enough combined memory and swap to hold every page of the private mapping, and you write to every page, then you (or possibly some other process) will be killed by the out-of-memory daemon.
Update: what I wrote above applies to memory-mapped files. You can map an anonymous block (
MAP_ANONYMOUS
) withMAP_SHARED
, in which case the memory is backed by swap, not a file.