内存优化读/写文件 [英] Memory optimization read/write file

查看:52
本文介绍了内存优化读/写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!读写文件时遇到麻烦.目前,我正在使用此代码;

Hi! I Have some trouble when reading and writing a file. At the moment I'm using this code;

using (Stream dest = File.Create("C:\test"))
using (Stream source = File.OpenRead(readFile))
{
	long fileLenght = new FileInfo(readFile).Length;
	byte[] buffer = new byte[35 * 1024];
	while (fileLenght > 0)
	{
		source.Read(buffer, 0, buffer.Length);
		fileLenght -= buffer.Length;
		dest.Write(buffer, 0, buffer.Length);
		dest.Flush();
	}
}

它只是复制文件"解决方案,足够简单.但是,当我将此代码应用于4GB的文件时,我的内存被杀死,稳定地达到了内存限制,然后我推测它会随着RAM再次下降到最低而将所有内容交换掉,然后 重复相同的模式,直到操作完成.可能是什么问题?为什么内存使用量只会增加?!

It is simply a "copy a file" solution, simple enough. However, when I apply this code to a 4GB file, my memory gets slaughtered, steadily reaching my memory limit, then I presume it throws it all to swap as the RAM goes down to bottom again, and then the same pattern is repeated all over until the operation is complete. What can be the problem? Why is the memoryusage only increasing?!

致谢!

推荐答案

您为什么不只使用File.Copy? microsoft.com/en-us/library/system.io.file.copy.aspx">http://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx
Why don't you just use File.Copy? http://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx


这篇关于内存优化读/写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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