1 year ago

#146402

test-img

juFo

C# MemoryMappedFile not enough resources

I'm doing operations on images. Some of these operations require me to create 3 different versions of the pixel data from the image and then later on combine them and do operantions on it.

For regular/small images the code works fine, I simply initialize my image raster data as new int[size].

However for bigger images with a bigger resolution (600, 1200, ...) the new int[size] throws an OutOfMemoryException. Trying to allocate more than 2GB. However I've built it 64bit (not anycpu or 32bit).

To resolve this issue, I've tried to create a MemoryMappedFile in memory itself. This gave me out of resource also. Next I've tried to create a MemoryMappedFile but by first creating a file on disk and then creating a accessor over the complete file.

Still I'm facing the not enough resources with the temporary file on disk and the MemoryMappedFile/ViewAcessor.

Am I doing something wrong in the code below? I thought the MMF and Accessor would handle the virtual memory paging automagically.

      mmfPath = Path.GetTempFileName();
      // create a file on disk first
      using (var fs = File.OpenWrite(mmfPath))
      {
        var widthBytes = new byte[width * 4]; 
        for (int y = 0; y < height; y++)
        {
          fs.Write(widthBytes, 0, widthBytes.Length);
        }
      }

      // open the file on disk as a MMF
      _RasterData = MemoryMappedFile.CreateFromFile(mmfPath,
          FileMode.OpenOrCreate,
          Guid.NewGuid().ToString(),
          0, // 0 to set te capacity to the size of the file on disk
            MemoryMappedFileAccess.ReadWrite);
      _RasterDataAccessor = _RasterData.CreateViewAccessor(); // <-- not enough memory resources
Not enough memory resources are available to process this command.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.MemoryMappedFiles.MemoryMappedView.CreateView(SafeMemoryMappedFileHandle memMappedFileHandle, MemoryMappedFileAccess access, Int64 offset, Int64 size)
at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateViewAccessor(Int64 offset, Int64 size, MemoryMappedFileAccess access)
at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateViewAccessor()
...

In case I can resolve the problem above, I think I will later on run again on the same issue when I need to create a resulting bitmap out of the pixeldata again. (2GB limit).

The goal is working with big images (and temporary copies of its pixeldata for raster/raster operations).

The current issue is that I'm getting Out of memory resources with MemoryMappedFile. Where I thought this would resolve the 2GB limit and that Windows/Framework would handle the virtual memory paging issues.

(.NET Framework 4.8 - 64bit build.)

c#

memory-mapped-files

0 Answers

Your Answer

Accepted video resources