将大文件写入磁盘内存不足异常 [英] Writing Large File To Disk Out Of Memory Exception

查看:70
本文介绍了将大文件写入磁盘内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写入然后读取一个大的随机文件以计算磁盘速度.我尝试了几种算法,但是在尝试写入1GB文件时总是出现内存不足或内存异常的情况.这是我尝试过的一些

I am attempting to write and then read a large random file to calculate disk speed. I have tried several algorithms but keep getting an out or memory exception when attempting to write a 1GB file. Here are a few I have tried

方法1

   byte[] data = new byte[8192];
        Random rng = new Random();
        using (FileStream stream = File.OpenWrite(filePath))
        {
            for (int i = 0; i < fileSizeMb * 128; i++)
            {
                rng.NextBytes(data);
                stream.Write(data, 0, data.Length);
            }
        }

方法二

const int blockSize = 1024 * 8;
            const int blocksPerMb = (1024 * 1024) / blockSize;

            int count = fileSizeMb * blocksPerMb;

            byte[] data = new byte[blockSize];
            Random rng = new Random();
            //using (FileStream stream = File.OpenWrite(filePath))
            using (StreamWriter sw1 = new StreamWriter(filePath))
            {
                // There 
                for (int i = 0; i < count; i++)
                {
                    rng.NextBytes(data);
                    sw1.BaseStream.Write(data, 0, data.Length);
                    //stream.Write(data, 0, data.Length);

                }
            }

阅读

   using (StreamReader sr = new StreamReader(filePath))
            {
                String line = sr.ReadToEnd();
            }

推荐答案

方法1

        byte[] data = new byte[8192];
        Random rng = new Random();
        using (FileStream stream = File.OpenWrite(filePath))
        {
            for (int i = 0; i < fileSizeMb * 128; i++)
            {
                rng.NextBytes(data);
                stream.Write(data, 0, data.Length);
                stream.Flush(); // BEETLE JUICE
            }
        }

方法2

        const int blockSize = 1024 * 8;
        const int blocksPerMb = (1024 * 1024) / blockSize;

        int count = fileSizeMb * blocksPerMb;

        byte[] data = new byte[blockSize];
        Random rng = new Random();
        using (StreamWriter sw1 = new StreamWriter(filePath))
        {
            // There 
            for (int i = 0; i < count; i++)
            {
                rng.NextBytes(data);
                sw1.BaseStream.Write(data, 0, data.Length);
                sw1.baseStream.Flush(); // BEETLE JUICE
            }
        }

阅读

不要将整个文件读入内存,一次只能读4096字节. http://www.csharp-examples.net/filestream-read-file中的示例代码/

Do not read the whole file into memory, just read 4096 bytes at a time. Sample code at http://www.csharp-examples.net/filestream-read-file/

这篇关于将大文件写入磁盘内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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