byte[] and efficiently passing by reference

匆匆过客 提交于 2019-12-01 17:41:49

Step 1:

var FileToProcess = new byte[maxDataSize];

Step 3:

FileToProcess = new BiztalkBinariesData()
    .Get(currentDocument.SubmissionSetId, currentDocument.FullFileName);

Your step 1 is completely unnecessary, since you re-assign the array in step 3 - you are creating a new array, you do not populate the existing array - So essentially step 1 is just creating more work for the GC, which if you do it in quick order (and if it is not optimized away by the compiler, which is entirely possible) might explain some of the memory pressure you are seeing.

Arrays are reference types and as such you will be passing a copy of the reference, not a copy of the array itself. That would only be true with value types.

This simple snippet illustrates how arrays behave as reference types:

public void Test()
{    
    var intArray = new[] {1, 2, 3, 4};
    EditArray(intArray);
    Console.WriteLine(intArray[0].ToString()); //output will be 0
}

public void EditArray(int[] intArray)
{
    intArray[0] = 0;
}

It will use the existing reference, don't worry. The array's contents aren't copied.

Your problem could be in the implementation and usage of the "BiztalkBinariesData" class.

I'm not sure how it is implemented but I do see you're declaring a new instance each time

new BiztalkBinariesData()

Something to think about..

I'm having OutOfMemoryExceptions and I feel like it's because we're instantiating too many byte array's

No, it is because yo allocate LARGE arrays. Limit them to like 48kb or 64kb and "combine" them with a custom container. 64kb means you can take the higher 2 bytes of the index to determine the array to use. The container contains an arrays of arrays. Handling very large objects leads to fragemntation and the inability to allocate one large array later down.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!