相关疑难解决方法(0)

为什么即使有大约700Mb的RAM空闲,我也会得到System.OutOfMemoryException?

我正在运行一些测试,看看我的日志记录将如何执行而不是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)

c# out-of-memory winforms

5
推荐指数
1
解决办法
6273
查看次数

标签 统计

c# ×1

out-of-memory ×1

winforms ×1