byte [] var = new byte []抛出OutOfMemory异常 [英] byte[] var = new byte[] throw OutOfMemory exception

查看:164
本文介绍了byte [] var = new byte []抛出OutOfMemory异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经通过调用File.ReadAllBytes来读取文件,但是当我读取大文件时我得到了一个OutOfMemory异常。



所以我尝试了FileStream并成功阅读700MB文件到字节数组。



I used to read file by calling File.ReadAllBytes, but I got an OutOfMemory exception when I read large file.

So I tried FileStream and succeed in reading 700MB file into byte array.

byte[] FileByte = null;
byte[] ResByte = null;

private void ReadFile(string FilePath)
        {
            using (FileStream fs = new FileStream(this.txtFilePath.Text, FileMode.Open, FileAccess.Read))
            {
                int length = (int)fs.Length;
                this.FileByte = new byte[length];
                int count;                         
                int sum = 0;                          

                while ((count = fs.Read(this.FileByte, sum, length - sum)) > 0)
                    sum += count;
            }



但是当我尝试下面的代码时,我得到了另一个OutOfMemory异常。


But when I tried the code below, I got another OutOfMemory exception.

this.ResByte = new byte[this.FileByte.Length];





FileByte的长度约为700.000 .000,我确信.NET数组最多可以加载2GB的字节。而且,第一个字节数组成功加载。那么为什么第二个字节数组会抛出OutOfMemory异常呢?



解释会有所帮助。 :)



The length of FileByte is about 700.000.000, and I'm sure .NET array can load up to 2GB of bytes. Moreover, the first byte array successfully loaded. So how come the second byte array throw OutOfMemory exception?

An explanation will be helpful. :)

推荐答案

检查文件的实际大小:任何.NET对象都限制为2GB,是的,所以你不能创建一个大于那个的字节数组,但在正常情况下700MB阵列不是问题。



如果文件肯定是700MB,那么你需要查看机器中有多少内存,可用的数量和硬盘上的可用空间。



尝试使用较小的文件 - 一兆或类似的代码 - 看看会发生什么。可能是你分配的内存超出了可以提供的内存。

我刚用699Mb文件和一个1.4Gb文件尝试了你的代码,它在699上工作正常,但是失败了1.4GB,即使我试过这个:

Check the actual size of your file: any .NET object is restricted to 2GB, yes, so you can't create a byte array larger that that, but a 700MB array is not a problem under normal circumstances.

If the file is definitely 700MB, then you need to look at how much memory you have in your machine, how much is available and the free space on your HDD.

Try your code with a smaller file - one meg or similar - and see what happens. It may be that you are allocating more memory than can be provided.
I just tried your code with a 699Mb file, and a 1.4Gb file, and it worked fine with the 699, but failed with the 1.4GB, even if I tried this:
FileInfo fi = new FileInfo(strFile);
FileByte = new byte[fi.Length];

即。没有涉及流。

您可能值得检查您正在构建的目标:X86将具有比64位应用程序更小的对象大小限制。



我建议你需要看看这个块,而不是把它看作一个大块的块。

I.e. without the stream involved.
It may be worth your checking what target you are building for: X86 will have a smaller "object size" limit than 64 bit applications.

I would suggest that you need to look at doing this in chunks, rather than reading it as a single massive lump anyway.


这篇关于byte [] var = new byte []抛出OutOfMemory异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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