内存映射文件的优点是什么? [英] What are the advantages of memory-mapped files?

查看:514
本文介绍了内存映射文件的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个项目的内存映射文件,并希望从以前使用过的人,或决定不使用它们,为什么?



<我特别关注以下内容,按照重要程度排列:


  • 并发

  • >随机存取

  • 效能

  • 易用性

  • 便携性

解决方案

我认为优点是您可以减少传统读取文件所需的数据复制量。 >

如果您的应用程序可以在内存映射文件中就地使用数据,则它可以进入而不被复制;如果您使用系统调用(例如Linux的pread()),那么通常会涉及内核将数据从自己的缓冲区复制到用户空间。这额外的复制不仅需要时间,而且通过访问这个额外的数据副本来降低CPU缓存的效率。如果数据实际上必须从光盘(如在物理I / O中),那么操作系统仍然需要读取它们,页面错误可能并不比系统调用更好,但如果它们不存在(即已经在OS缓存中),理论上性能应该更好。

缺点是没有内存映射文件的异步接口 - 如果你试图访问一个不是它会产生一个页面错误,然后让线程等待I / O。




映射文件位于32位操作系统上 - 您可以轻松地使用地址空间。


I've been researching memory mapped files for a project and would appreciate any thoughts from people who have either used them before, or decided against using them, and why?

In particular, I am concerned about the following, in order of importance:

  • concurrency
  • random access
  • performance
  • ease of use
  • portability

解决方案

I think the advantage is really that you reduce the amount of data copying required over traditional methods of reading a file.

If your application can use the data "in place" in a memory-mapped file, it can come in without being copied; if you use a system call (e.g. Linux's pread() ) then that typically involves the kernel copying the data from its own buffers into user space. This extra copying not only takes time, but decreases the effectiveness of the CPU's caches by accessing this extra copy of the data.

If the data actually have to be read from the disc (as in physical I/O), then the OS still has to read them in, a page fault probably isn't any better performance-wise than a system call, but if they don't (i.e. already in the OS cache), performance should in theory be much better.

On the downside, there's no asynchronous interface to memory-mapped files - if you attempt to access a page which isn't mapped in, it generates a page fault then makes the thread wait for the I/O.


The obvious disadvantage to memory mapped files is on a 32-bit OS - you can easily run out of address space.

这篇关于内存映射文件的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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