locked
System.OutOfMemoryException RRS feed

  • Question

  • I have another problem with concat two byte[] on of them have more than 300,000,000 byte , it's makeException of type 'System.OutOfMemoryException' was thrown i use this code : byte[] b3 = by2.Concat(by1).ToArray();
    • Edited by mariosoft Sunday, February 9, 2014 7:50 AM
    Sunday, February 9, 2014 7:50 AM

Answers

  • Don't use Concat/ToArray on such large arrays, it's horrible with regards to memory usage and overall performance.

    Create an array of the required size and copy the other 2 arrays in it:

    byte[] a = new byte[a1.Length + a2.Length];
    Buffer.BlockCopy(a1, 0, a, 0, a1.Length);
    Buffer.BlockCopy(a2, 0, a, a1.Length, a2.Length);
    

    But even so the amount of used memory is very high and there's a good chance that this will fail in a 32 bit app. If you really need to deal with such large arrays you should use 64 bit instead.

    Best thing would be to not have to concatenate those arrays in the first place.

    Sunday, February 9, 2014 10:19 AM

All replies

  • Hello,

    How much memory the overall application is taking?


    Regards, Eyal Shilony

    Sunday, February 9, 2014 8:45 AM
  • Don't use Concat/ToArray on such large arrays, it's horrible with regards to memory usage and overall performance.

    Create an array of the required size and copy the other 2 arrays in it:

    byte[] a = new byte[a1.Length + a2.Length];
    Buffer.BlockCopy(a1, 0, a, 0, a1.Length);
    Buffer.BlockCopy(a2, 0, a, a1.Length, a2.Length);
    

    But even so the amount of used memory is very high and there's a good chance that this will fail in a 32 bit app. If you really need to deal with such large arrays you should use 64 bit instead.

    Best thing would be to not have to concatenate those arrays in the first place.

    Sunday, February 9, 2014 10:19 AM
  • If you're using .NET 4.5 and the application requirement requires consumers to have x64 bit machines then maybe you can try to use gcAllowVeryLargeObjects.


    Regards, Eyal Shilony

    Sunday, February 9, 2014 10:45 AM