我正在运行一些测试,看看我的日志记录将如何执行而不是File.AppendAllText
我先写入内存流然后复制到文件.所以,只是为了看看内存操作有多快我就这样做了..
private void button1_Click(object sender, EventArgs e)
{
using (var memFile = new System.IO.MemoryStream())
{
using (var bw = new System.IO.BinaryWriter(memFile))
{
for (int i = 0; i < Int32.MaxValue; i++)
{
bw.Write(i.ToString() + Environment.NewLine);
}
bw.Flush();
}
memFile.CopyTo(new System.IO.FileStream(System.IO.Path.Combine("C", "memWriteWithBinaryTest.log"), System.IO.FileMode.OpenOrCreate));
}
}
Run Code Online (Sandbox Code Playgroud)
当i
达到25413324
我得到了Exception of type 'System.OutOfMemoryException' was thrown.
,即使我的Process Explorer的说,我对自由的RAM 700MB ???
这是屏幕截图(以防万一)
Process Explorer
这是winform
编辑:为了在堆上创建更多对象,我重写了bw.write
这个
bw.Write(i);
Run Code Online (Sandbox Code Playgroud)