We load a 222 MB file into a MemoryMappedFile to access the raw data. This data is updated using the write method. After some calculations, the data should be reset to the original file value. We are currently doing this by deleting the class and creating a new instance. This happens many times, but sometimes CreateViewAccessor fails with the following exception:
System.Exception: There is not enough memory to process this command. ---> System.IO.IOException: There is not enough memory to process this command.
in System.IO .__ Error.WinIOError (Int32 errorCode, String maybeFullPath) in System.IO.MemoryMappedFiles.MemoryMappedView.CreateView (SafeMemoryMappedFileHandle> memMappedFileHandle, access to MemoryMappedFileAccess, offset Int64, size Int64) in System.IO.MemoryMileMile.Mile.Mile.Mile.Mile.Mile.Mile.Mile.FileMile.Mile.Mile.FileMile.Mile.Mile.FileMile.Mile.MileMile.FileMiles.Mile.FileMile.MileMile.FileMiles.Mile.FileMile.MileMile.FilesMile.Mile.FilesM.MileMileFiles.MileMile.FilesM.MileFiles.MileMile.Files.MileMile.Files.MileMile.Files.Mile.FileMiles.Mile.Files.MemoryMileFilesMapped.FileMapped CreateViewAccessor (Int64 size, Int64 size>, access to MemoryMappedFileAccess)
The following class is used to access a file with memory:
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)); } }
What are the possible causes of this problem and is there any solution / workaround?
source share