将大文件写入磁盘内存不足异常
Writing Large File To Disk Out Of Memory Exception
我正在尝试写入然后读取一个大的随机文件来计算磁盘速度。我已经尝试了几种算法,但在尝试写入 1GB 文件时不断出现 out 或内存异常。这是我试过的几个
方法一
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();
}
方法一
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
}
}
方法二
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/
处的示例代码
我正在尝试写入然后读取一个大的随机文件来计算磁盘速度。我已经尝试了几种算法,但在尝试写入 1GB 文件时不断出现 out 或内存异常。这是我试过的几个
方法一
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();
}
方法一
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
}
}
方法二
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/
处的示例代码