如何动态地扩展内存映射文件内存、文件、动态

2023-09-07 12:38:00 作者:今天也很喜欢你

我用C#来解决followering要求.. - 创建一个应用程序的可recive大量数据快 - 你必须能够分析:收到的数据,而更多的人进来的。 - 用尽可能少的CPU和盘地

I've used C# to solve the followering requirement.. - create an app the can recive a lot of data fast - you must be able to analyse the recieved data while more are incomming. - use as little cpu and disc as possible

我的想法一种算法是..

My idea for an algorithm was..

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的chuncks增加时,previous间/空间用于

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

是如何提高mmf.size按大小,在C#中做了什么?我发现了很多简单的例子创造货币市场基金和意见,但唯一的地方(link)我见过code,它实际上可以增加货币市场基金区域使用code不能编译。任何帮助将大大appriciated。

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:\用户\莫伯格\文档\ data.bin',因为它正由另一个进程使用

This exception is thrown : The process cannot access the file 'C:\Users\moberg\Documents\data.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.