何时使用内存映射文件? [英] When to use memory-mapped files?

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

问题描述

我有接收的数据块通过网络,并写入这些磁盘的应用程序。
一旦所有数据块已经收到,他们可以被解码/重组到他们实际上代表了一个文件。

I have an application that receives chunks of data over the network, and writes these to disk. Once all chunks have been received, they can be decoded/recombined into the single file they actually represent.

我不知道,如果是使用存储有用-mapped文件或不 - 第一次写单块磁盘,第二个用于在其中所有的人都解码单个文件

I'm wondering if it's useful to use memory-mapped files or not - first for writing the single chunks to disk, second for the single file into which all of them are decoded.

我自己的感觉是,它可能对于只有第二种情况是有用的,任何人有一些想法在这个

My own feeling is that it might be useful for the second case only, anyone got some ideas on this?

编辑:
这是一个C#应用程序,我只打算在x64版本。
(所以跑步进入最大contigious自由空间的问题应该是不相关的)

It's a C# app, and I'm only planning an x64 version. (So running into the 'largest contigious free space' problem shouldn't be relevant)

推荐答案

内存映射文件是在需要重复访问一个相当大的文件的相对小的部分(图)的方案是有利的。

Memory-mapped files are beneficial for scenarios where a relatively small portion (view) of a considerably larger file needs to be accessed repeatedly.

在这种情况下,操作系统可以帮助在通过寻呼优化应用程序的整体内存使用和寻呼行为和出仅映射文件的最近使用的部分

In this scenario, the operating system can help optimize the overall memory usage and paging behavior of the application by paging in and out only the most recently used portions of the mapped file.

此外,内存映射文件会暴露有趣的功能,如写入时复制或作为共享内存的基础。

In addition, memory-mapped files can expose interesting features such as copy-on-write or serve as the basis of shared-memory.

有关你的情况下,内存映射文件可以帮你组装文件,如果块没有按顺序到达。不过,你仍然需要预先知道最终的文件大小。

For your scenario, memory-mapped files can help you assemble the file if the chunks arrive out of order. However, you would still need to know the final file size in advance.

此外,你应该只有一次访问该文件,写一大块。因此,性能优势显式实现异步I / O的可能性不大,但它可能会更容易和更快地正确地贯彻执行文件的作家。

Also, you should be accessing the files only once, for writing a chunk. Thus, a performance advantage over explicitly implemented asynchronous I/O is unlikely, but it may be easier and quicker to implement your file writer correctly.

在.NET 4中,微软增加了对内存映射文件的支持,并有示例代码,如一些综合性文章 HTTP: //blogs.msdn.com/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx

In .NET 4, Microsoft added support for memory-mapped files and there are some comprehensive articles with sample code, e.g. http://blogs.msdn.com/salvapatuel/archive/2009/06/08/working-with-memory-mapped-files-in-net-4.aspx.

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

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