So this is in relationship to dealing with the Large Object Heap and trying to minimize the number of times I instantiate a byte[]. Basically, I'm having OutOfMemoryExceptions and I feel like it's because we're instantiating too many byte array's. The program works fine when we process a couple of files, but it needs to scale, and it currently can't.
In a nutshell, I've got a loop that pulls documents from a database. Currently, it's pulling one document at a time and then processing the document. Documents can range from less than a meg to 400+ megs. (hence why i'm processing one at a time). The following is pseudo-code and before I've optimized.
So the steps I'm doing are:
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);
Was that clear? If not, I'll try to clean it up.