将字节数组转换为字符串时出现内存不足异常 [英] Out of Memory exception while converting byte array into string

查看:110
本文介绍了将字节数组转换为字符串时出现内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将字节数组转换为字符串时出现内存异常。最初我以varbinay格式将文件内容(338 MB)存储到DataBase中,在读取DataBase中的内容后,我将Byte数组转换为字符串,我收到了错误。

 var val = cmd.ExecuteScalar(); //获取表单数据库
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string myString = enc.GetString((byte [])val); //转换为字符串



请有人告诉我一个解决方案。



谢谢&此致,

Ramana

解决方案

您需要将字符串拆分为块并单独处理块。有一个很好的例子说明如何做到这一点这里 [ ^ ]。



希望这有助于

我要尝试的第一件事就是用 SMALL 文件做同样的事情。



如果这样可以正常工作,那么很可能确保您的数据量达到338 MB。你将不得不阅读和转换它(可能每次读取/转换100k ......)。


我刚才遇到同样的问题我得到了这个问题当我尝试谷歌搜索解决方案。幸运的是,我带来了解决方案这里 [ ^ ]。

- ps:考虑在类声明中给出final修饰符。


Hi,
I'm getting out of memory exception while converting byte array into a string. Initially I've stored content of a file(338 MB) into DataBase in varbinay format , after reading the content from DataBase I'm conveting that Byte array into string wher I'm getting the error .

var val = cmd.ExecuteScalar();//Getting form DataBase
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string myString = enc.GetString((byte[])val);//Converting into String


Please someone tell me a solution.

Thanks & Regards,
Ramana

解决方案

You will need to break up the string into chunks and process the chunks individually. There is a good example of how to do this Here[^].

Hope this helps


The first thing I would try is do the same with a SMALL file.

If this works fine, it's pretty sure that the amount of your data 338 MB is to big. You will have to read and convert it in pieces (maybe 100k per read/convert ...).


Hi, I'm having the same problem just now and I got to this question when I try googling for solution. Fortunately, I came with the solution here[^].
-ps:Consider giving final modifier in class declaration.


这篇关于将字节数组转换为字符串时出现内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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