字节[]和由参考有效传递 [英] byte[] and efficiently passing by reference

查看:146
本文介绍了字节[]和由参考有效传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这是在相对于与大对象堆处理并试图最小化I实例化一个字节[]的次数。基本上,我有OutOfMemoryExceptions,我觉得这是因为我们实例太多的字节数组的。该项目工程优良,当我们处理一些文件,但它需要扩展,目前它不能。

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.

在概括地说,我有一个循环,从数据库中拉文档。目前,它的拔一个文档的时间和然后处理文档。文档的范围可以从不到梅格400+兆。 (因此为什么我处理一次)。以下是伪code和我之前的优化

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.

所以我做的步骤是:


  1. 请与数据库的调用来发现的最大文件大小(然后由1.1乘以)

  1. 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];


  • 然后我做从数据库中另一个数据库调用拉动的所有文件(无数据),并把这些变成一个IEnumerable

  • 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
    }
    


  • 然后我填充我的byte []从外部源数组:

  • Then I populate my byte[] array from an external source:

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


  • 下面是个问题。这将是更清洁的currentDocument(ICla​​imDocument)传递给其他方法来处理。因此,如果我的currentDocument的数据部分设置为$ P $对格式化的阵列,将这种使用现​​有的参考?或者这是否建立在大对象堆一个新的数组?

  • 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;
    


  • 在循环结束后,我会再清楚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.

    推荐答案

    第1步:

    var FileToProcess = new byte[maxDataSize];
    

    步骤3:

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

    您第1步是完全没有必要的,因为您在步骤3中的阵列重新分配 - 您正在创建的新的的数组,你不填充现有阵列 - 所以基本上第1步是刚刚创建对于GC,如果你做这在快速为了它(如果它不是由编译器,这是完全有可能的优化掉的),更多的工作可以解释一些你所看到的内存pressure的。

    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.

    这篇关于字节[]和由参考有效传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆