簡體   English   中英

拋出了“System.OutOfMemoryException”類型的異常。 C# 使用內存流時

[英]Exception of type 'System.OutOfMemoryException' was thrown. C# when using Memory stream

我正在使用 wpf 應用程序和內存流寫入方法用於寫入 dicom 數據字節。當嘗試寫入大小超過 70 mb 的大 dicom 數據時,它顯示System.OutOfMemoryException類型的異常。 您能否提出任何解決方案來解決此問題。

這段代碼是這樣的

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 );
            }

由於缺少可用的連續(非全部)內存,MemoryStream 拋出 OutOfMemoryExceptions 是很常見的。 有許多替代實現可以減輕這個問題。 MemoryTributary為例。

或者,根據您的需要,您可以嘗試直接寫入存儲而不是內存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM