我已经使用 C# 解决了以下需求.. - 创建一个可以快速接收大量数据的应用程序 - 您必须能够在更多数据传入时分析接收到的数据。 - 使用尽可能少的 CPU 和磁盘
我对算法的想法是..
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 为单位增加。
如何在 C# 中完成“将 mmf.size 增加 SIZE”?我发现了很多关于创建 mmfs 和 View 的简单示例,但唯一的地方 ( link ) 我看到的代码实际上增加了 mmfs 区域使用无法编译的代码。任何帮助将不胜感激。
编辑 这会导致异常:
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:\Users\moberg\Documents\data.bin”,因为它正被另一个进程使用。
最佳答案
Once you map a file in memory, you cannot increase its size.这是内存映射文件的已知限制。
...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.
一种策略是使用存储在 non-persisted memory mapped files of a given size 中的 block ,比如 1GB 或 2GB。您将通过您自己设计的顶级 ViewAccessor
来管理这些(可能从 MemoryMappedViewAccessor
执行您需要的方法的基本直通)。
编辑:或者您可以只创建一个您希望使用的最大大小的非持久内存映射文件(比如 8GB 开始,并在您的系统启动时使用一个参数对其进行调整应用程序)并检索每个逻辑 block 的 MemoryMappedViewAccessor
。在请求每个 View 之前,非持久化文件不会使用物理资源。
关于c# - 如何动态扩展内存映射文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6096485/