我们正在加载一个用于原始数据访问的MemoryMappedFile中的222MB文件。该数据使用写入方法进行更新。经过一些计算后,数据应重置为文件的原始值。我们目前正在通过处理这个类并创建一个新实例来做到这一点。 这远远很多次了,但有时CreateViewAccessor与以下异常崩溃:MemoryMappedFile CreateViewAccessor抛出“没有足够的存储可用于处理此命令。”
System.Exception的:没有足够的存储是可用来处理此命令。 ---> System.IO.IOException:没有足够的存储可用于处理此命令。
在System.IO .__ Error.WinIOError(的Int32的errorCode,字符串maybeFullPath) 在System.IO.MemoryMappedFiles.MemoryMappedView.CreateView(SafeMemoryMappedFileHandle> memMappedFileHandle,MemoryMappedFileAccess访问,Int64的偏移,Int64的大小) 在System.IO。 MemoryMappedFiles.MemoryMappedFile.CreateViewAccessor(Int64的偏移量,Int64类型>大小,MemoryMappedFileAccess访问)
下面的类被用于访问存储器映射的文件:
public unsafe class MemoryMapAccessor : IDisposable
{
private MemoryMappedViewAccessor _bmaccessor;
private MemoryMappedFile _mmf;
private byte* _ptr;
private long _size;
public MemoryMapAccessor(string path, string mapName)
{
FileInfo info = new FileInfo(path);
_size = info.Length;
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
_mmf = MemoryMappedFile.CreateFromFile(stream, mapName, _size, MemoryMappedFileAccess.Read, null, HandleInheritability.None, false);
_bmaccessor = _mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.CopyOnWrite);
_bmaccessor.SafeMemoryMappedViewHandle.AcquirePointer(ref _ptr);
}
public void Dispose()
{
if (_bmaccessor != null)
{
_bmaccessor.SafeMemoryMappedViewHandle.ReleasePointer();
_bmaccessor.Dispose();
}
if (_mmf != null)
_mmf.Dispose();
}
public long Size { get { return _size; } }
public byte ReadByte(long idx)
{
if ((idx >= 0) && (idx < _size))
{
return *(_ptr + idx);
}
Debug.Fail(string.Format("MemoryMapAccessor: Index out of range {0}", idx));
return 0;
}
public void Write(long position, byte value)
{
if ((position >= 0) && (position < _size))
{
*(_ptr + position) = value;
}
else
throw new Exception(string.Format("MemoryMapAccessor: Index out of range {0}", position));
}
}
这个问题有什么可能的原因,有没有解决方法/解决方法?
你试过在处理MMF后调用'GC.Collect'吗?那个错误通常意味着没有足够的内存。 – 2013-03-27 15:48:50
我们已经写完了我们自己的映射器,它将这个文件的块加载到内存中,因此不需要在内存中有一大块可用空间。你可以查看https://gist.github.com/luuksommers/af473efe618d589df951 – Luuk 2015-03-06 09:47:03