这段内容涉及到处理大对象堆(Large Object Heap),并尝试最小化实例化byte[]的次数。我遇到了OutOfMemoryExceptions异常,感觉是因为我们实例化了过多的byte数组。当我们处理几个文件时,程序可以正常运行,但需要扩展规模,目前还无法做到。
简而言之,我有一个循环从数据库中提取文档。目前,它一次只提取一个文档,然后处理该文档。文档的大小可以从不到1MB到超过400MB不等。(因此我一次只处理一个)。以下是伪代码,在我进行优化之前。
所以我正在执行以下步骤:
Make a call to the database to find the largest file size (and then multiplying it by 1.1)
var maxDataSize = new BiztalkBinariesData().GetMaxFileSize(); maxDataSize = (maxDataSize != null && maxDataSize > 0) ? (long)(maxDataSize * 1.1) : 0; var FileToProcess = new byte[maxDataSize];
Then I make another database call pulling all of the documents (without data) from the database and place these into an IEnumerable.
UnprocessedDocuments = claimDocumentData.Select(StatusCodes.CurrentStatus.WaitingToBeProcessed); foreach (var currentDocument in UnprocessDocuments) { // all of the following code goes here }
Then I populate my byte[] array from an external source:
FileToProcess = new BiztalkBinariesData() .Get(currentDocument.SubmissionSetId, currentDocument.FullFileName);
Here is the question. It would be much cleaner to pass the currentDocument (IClaimDocument) to other methods to process. So if I set the data part of the currentDocument to the pre-formatted array, will this use the existing reference? Or does this create a new array in the Large Object Heap?
currentDocument.Data = FileToProcess;
At the end of the loop, I would then clear FileToProcess
Array.Clear(FileToProcess, 0, FileToProcess.length);
UnprocessedDocuments
或其项是否实现了IDisposable
接口? - sllOutOfMemoryException
异常?这个异常是否在一个相对固定的位置被抛出,还是看起来是随机的? - Branko Dimitrijevic