如何动态扩展内存映射文件 [英] How to dynamically expand a Memory Mapped File

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

问题描述

我已经使用 C# 来解决以下需求..- 创建一个可以快速接收大量数据的应用程序- 您必须能够在接收到更多数据时分析接收到的数据.- 使用尽可能少的 CPU 和磁盘

I've used C# to solve the following requirement.. - create an app the can receive a lot of data fast - you must be able to analyse the received data while more are incoming. - use as little CPU and disk as possible

我对算法的想法是..

SIZE = 10MB
Create a mmf with the size of SIZE
On data recived:
  if data can't fit mmf: increase mmf.size by SIZE
  write the data to mmf

-> 使用之前的房间/空间"时,光盘上的大小以 10MB 为单位增加.

-> The size on the disc are increased in chuncks of 10MB when the previous "room/space" are used.

如何在 C# 中实现按 SIZE 增加 mmf.size"?我发现了很多关于创建 mmfs 和视图的简单示例,但唯一的地方(link) 我见过实际增加 mmfs 区域的代码使用无法编译的代码.任何帮助将不胜感激.

How is the "increase mmf.size by SIZE" done in C#? I have found a lot of simple examples on creating mmfs and views but the only place (link) I have seen code that acutally increases the mmfs area uses code that can't compile. Any help will be greatly appriciated.

编辑这会导致异常:

private void IncreaseFileSize()
{
    int theNewMax = this.currentMax + INCREMENT_SIZE;
    this.currentMax = theNewMax;

    this.mmf.Dispose();

    this.mmf = MemoryMappedFile.CreateFromFile(this.FileName, FileMode.Create, "MyMMF", theNewMax);
    this.view = mmf.CreateViewAccessor(0, theNewMax);            
}

抛出此异常:进程无法访问文件C:UsersmobergDocumentsdata.bin",因为它正被另一个进程使用.

This exception is thrown : The process cannot access the file 'C:UsersmobergDocumentsdata.bin' because it is being used by another process.

推荐答案

一旦将文件映射到内存中,就无法增加其大小.这是内存映射文件的已知限制.

Once you map a file in memory, you cannot increase its size. This is a known limitation of memory mapped files.

...您必须计算或估计完成的文件的大小,因为文件映射对象的大小是静态的;一旦创建,它们的大小就不能增加或减少.

...you must calculate or estimate the size of the finished file because file mapping objects are static in size; once created, their size cannot be increased or decreased.

一种策略是使用存储在 非持久内存映射文件中的块给定的大小,比如 1GB 或 2GB.您将通过您自己设计的顶级 ViewAccessor 管理这些(可能从 MemoryMappedViewAccessor).

One strategy would be to use chunks stored in non-persisted memory mapped files of a given size, say 1GB or 2GB. You would manage these through a top level ViewAccessor of your own design (probably doing basic passthru of the methods you need from the MemoryMappedViewAccessor).

或者您可以创建一个您希望使用的最大大小的非持久内存映射文件(比如 8GB 开始,使用一个参数在您的系统启动时对其进行调整)应用程序)并检索每个逻辑块的 MemoryMappedViewAccessor.在请求每个视图之前,非持久化文件不会使用物理资源.

or you could just create a non-persisted memory mapped file of a maximal size you expect to use (say 8GB to start, with a parameter to tune it on start-up of your application) and retrieve MemoryMappedViewAccessor's per logical chunk. The non-persisted file will not use physical resources until each view is requested.

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

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