concat两个byte []返回System.OutOfMemoryException [英] concat two byte[] returns System.OutOfMemoryException

查看:143
本文介绍了concat两个byte []返回System.OutOfMemoryException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有CONCAT两字节的[]的一个问题。其中一人有3亿字节。它的投掷型的System.OutOfMemoryException

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.

我使用此代码的异常:

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



有人能帮助我

anybody can help me

推荐答案

。它不能创造适宜的大阵,只是用数据填充它。因此它创建小一个,那么当它的全创建新的两倍的大小,等等一遍一遍只要有更多的数据来填充。这样,你需要更多的内存,则只是理论上的(b1.Length + b2.Length)* 2 。而且事情变得更加棘手,因为某一点后,这些大阵列上蕙分配,而不是收集,很容易通过GC正常的对象。

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.

这就是为什么你应该不要使用 ToArray的()在这种情况下,并做了老式的方法:用大小等于分配新的阵列组合光源阵列的尺寸和复制数据

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.

是这样的:

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



这并不保证成功,但使得它更容易。并执行很多很多,要快得多那么 ToArray的()

这篇关于concat两个byte []返回System.OutOfMemoryException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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