我正在運行一些測試,看我的日誌記錄將如何執行,而不是做File.AppendAllText
我會先寫入內存流,然後複製到文件。所以,只是爲了看看如何快速記憶操作我這樣做..即使有大約700Mb的RAM空間,爲什麼我會得到System.OutOfMemoryException?
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));
}
}
當i
達到25413324
我得到了Exception of type 'System.OutOfMemoryException' was thrown.
即使我Process Explorer說,我對自由的RAM 700MB ???
下面是屏幕截圖(以防萬一)
進程瀏覽器
這裏的WinForm的
編輯:對於多個對象的緣故被創建在堆上,我將bw.write
重寫爲這個
bw.Write(i);
根據回答,'BinaryWriter'不寫出數字的字符串表示形式。它將寫入字節表示。也許你想要後者,但從原來的代碼它並不出現如此。 – leppie 2013-03-28 11:21:08