1

I am using Memory Mapped Files to read data from binary file like below but getting error :

Error : Not Enough Storage Is Available To Process This Command.

public void Execute()
 {
   MemoryMappedFile dataMmf;
   dataMmf = MemoryMappedFile.CreateFromFile(Path.Combine(folder, "Data.bin"));
   dataStream = dataMmf.CreateViewStream();
   BinaryReader reader = new BinaryReader(dataStream, Encoding.UTF8, true); 
   // Code .......
 }

I am working with following application :

  • Mvc Application

  • Class Library(MyEngine.dll)

Mvc Application contains MyEngine.dll and i am calling Execute method from my mvc application.

Memory Mapped file requires 64 bit environment to run so i guess when i call Execute method from mvc app,Execute method runs inside web server.

But when i go to properties of my mvc app there i see : Prefer 32 bit option as unchecked and disable

Then why i am getting storage error.This storage error occurs when memory mapped file is run inside 32 bit environment.

MyEngine.dll also have this option as unchecked : Prefer 32 bit

Strange things is i dont get error in case of 100,000 records but when i read 12 millions records then i get error.

On Msdn there is nothing mention about limit that for 32 bit it will read up to this much data.

The way i resolved this error is like below :

enter image description here

But i am trying to understand that why it worked after setting above option only and what i have to do when i will publish this solution on server?

Is there any other way to solve this without setting above option?

Size of data.bin = 448 mb

Stack Trace :

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.CreateViewStream(Int64 offset, Int64 size, MemoryMappedFileAccess access)
   at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateViewStream()
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • How large is `data.bin`? – mjwills Mar 15 '18 at 10:58
  • @mjwills Size of data.bin is 448 mb – I Love Stackoverflow Mar 15 '18 at 11:01
  • What is the stack trace of that exception? A MemoryMappedFile doesn't require memory the size of the file to be available, depending on what you're doing with the file. – CodeCaster Mar 15 '18 at 12:08
  • @CodeCaster I updated my question to include stack trace.I am just reading data from the binary file indirectly through the Memory mapped file – I Love Stackoverflow Mar 15 '18 at 12:13
  • Yeah sorry but still a non-repro. Read [ask] and create a [mcve]. A MMF of a file of 450 MB does not require 450 MB of RAM. You're probably calling this method a number of times and not properly disposing earlier views or files. – CodeCaster Mar 15 '18 at 12:18
  • @CodeCaster I have properly disposed file objects and suppose if i am not disposing properly this then how does my solution works after setting 64 bit option ? – I Love Stackoverflow Mar 15 '18 at 12:21
  • Because then you have more memory to pollute before you get that exception. The fact that you get this exception almost always means you're doing something wrong. Think about the scalability. What if two users perform this action at the same time? – CodeCaster Mar 15 '18 at 12:22
  • @CodeCaster When i call MyEngine.dll method inside console application and running 8 jobs in parallel then i dont get any exception.All those 8 parallel jobs even ran successfully – I Love Stackoverflow Mar 15 '18 at 12:27
  • Because your console app is 64 bit and has more memory to spare. The fact that code doesn't give an error under some cases doesn't mean it's correct. – CodeCaster Mar 15 '18 at 12:35

0 Answers0