3

I am working with wpf application and memory stream write method is used in that to write dicom data bytes.It shows the Exception of type System.OutOfMemoryException when try to write big dicom data having size more than 70 mb. Can you please suggest any solution to resolve this.

The piece of code is like this

try
            {
                using ( MemoryStream imagememoryStream = new MemoryStream())
                {
                    while (true)
                    {
                        // Retrieve the DICOMData.
                        // data comes as chunks; if file size is larger, multiple RetrieveDICOMData() calls
                        // has to be raised. the return value specifies whether the chunk is last one or not.                  
                        dicomData = dicomService.RetrieveDICOMData( hierarchyInfo );
                        imagememoryStream.Write( dicomData.DataBytes, 0, dicomData.DataBytes.Length );
                        if (dicomData.IsLastChunk)
                        {
                            // data is smaller; completed reading so, end
                            break;
                        }
                    }
                    imageData=imagememoryStream.ToArray();
                }
                return imageData;
            }
            catch( Exception exception )
            {
                throw new DataException( exception.StackTrace );
            }
6
  • 3
    Can you not write this data in chunks? Why must it be so large? Commented Aug 8, 2013 at 4:34
  • Can you post your code ? Commented Aug 8, 2013 at 4:35
  • I say sir, it does seem like your process is ... out of memory. Quite peculiar indeed! said no programmer who understood how much address space is accessible to a 32-bit process, ever. Commented Aug 8, 2013 at 4:42
  • Try to call GC.Collect(); inside the loop. Commented Aug 8, 2013 at 4:44
  • I have used GC.Collect() inside the loop , but same issue happens... Any alternates?? Commented Aug 8, 2013 at 5:42

1 Answer 1

3

It is quite common for MemoryStream to throw OutOfMemoryExceptions due to lack of contiguous (not total) memory available. There are a number of alternate implementations that lessen this problem. Take a look at MemoryTributary for example.

Or, depending on your needs, you could try writing direct to storage instead of memory.

Sign up to request clarification or add additional context in comments.

1 Comment

It is working fine.Thank you very much for your valuable suggestion. But I have a dependancy project in managed cpp, it shows "Not enough storage to process this command" exception. Can you suggest any solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.