MemoryMappedFile CreateViewAccessor throws "Not enough memory to process this command."

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?

+4
source share
1 answer
  • Try using the x64 platform and instead of x32

  • Make sure that you install MemoryMapAccessor manually each time. According to your implementation, GC will not be a Dispose call for you - here is a great explanation about this Proper use of the IDisposable interface

  • Calling Dispose does not make your variable null, so the GC will wait until it realizes that no one is using these variables. Make sure your variables go beyond Dispose or just mark them null. The simplest case is to get rid of your Dispose - why not mark the variables as null if you no longer need them? This allows the GC to eat them faster.

  • Here is another good topic regarding this error (although it is mentioned in the VS.Net IDE, it contains information about why such an error can occur) There is not enough memory to process this command in VisualStudio 2008 . One idea is if you often need really large pieces of memory, which leads to memory fragmentation, so soon, when you still have enough full free memory, you donโ€™t have enough free memory space.

  • In your particular case, maybe the idea of โ€‹โ€‹food is simply to read the byte[] array in memory from a file, although it does not contain deeply unmanaged resources. With some successful encoding, this can lead to better memory management using the CLR; but you must be careful with such decisions.

+5
source

All Articles