Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concat two byte[] returns System.OutOfMemoryException

Tags:

c#

I have a problem with concat two byte[]. One of them have more than 300,000,000 byte. It's throwing exception of type System.OutOfMemoryException.

I use this code :

byte[] b3 = by2.Concat(by1).ToArray();

anybody can help me

like image 544
MaRiO Avatar asked May 23 '26 13:05

MaRiO


1 Answers

Because of Concat call ToArray know nothing about how big the result array has to be. It can't create proper, big array and just fill it with data. So it creates small one, then when it's full creates new one with twice the size, etc. over and over again as long as there is more data to fill. This way you need much more memory then just theoretical (b1.Length + b2.Length) * 2. And things get even more tricky, because after certain point these big arrays are allocated on LOH, and are not collected that easily by GC as normal objects.

That's why you should not use ToArray() in this case and do it the old-fashioned way: allocate new array with size equals combines sizes of source arrays and copy the data.

Something like:

var b3 = new byte[b1.Length + b2.Length];
Array.Copy(b1, b2, b1.Length);
Array.Copy(b1, 0, b2, b1.Length, b2.Length);

It does not guaranty success, but makes it more likely. And executes much, much, much faster then ToArray().

like image 165
MarcinJuraszek Avatar answered May 26 '26 20:05

MarcinJuraszek