我正在实现一些适用于大数据(〜250 MB - 1 GB)的算法。为此,我需要一个循环来做一些基准测试。然而,在这个过程中,我了解到F#正在做一些令人讨厌的事情,我希望你们中的一些人能够澄清。F#编译器保持死对象存活
这里是我的代码(问题的描述如下):
open System
for i = 1 to 10 do
Array2D.zeroCreate 10000 10000 |> ignore
printfn "%d" (GC.GetTotalMemory(true))
Array2D.zeroCreate 10000 10000 |> ignore
// should force a garbage collection, and GC.Collect() doesn't help either
printfn "%d" (GC.GetTotalMemory(true))
Array2D.zeroCreate 10000 10000 |> ignore
printfn "%d" (GC.GetTotalMemory(true))
Array2D.zeroCreate 10000 10000 |> ignore
printfn "%d" (GC.GetTotalMemory(true))
Array2D.zeroCreate 10000 10000 |> ignore
printfn "%d" (GC.GetTotalMemory(true))
Console.ReadLine() |> ignore
这里输出将是这样的:
54000
54000
54000
54000
54000
54000
54000
54000
54000
54000
400000000
800000000
1200000000
Out of memory exception
因此,在循环F#丢弃结果,但是当我不在循环中F#会保留对“死数据”的引用(我已经在IL中看过,显然类程序会为这些数据获取字段)。为什么?我能解决这个问题吗?
此代码在Visual Studio之外运行并在发布模式下运行。
+1谢谢,有趣:)第一个解决方案有效,但忽略忽略在这里没有帮助。尽管如此,我仍然有兴趣找出它为什么会这样做。 – 2011-06-12 17:30:03
有趣......当我用'-O'选项尝试时(帮助优化),它有所帮助。 – 2011-06-12 18:30:08
奇怪。当我在Visual Studio之外运行时,它也在这里工作。但是,现在循环导致了内存不足的异常,但是只有当我先运行“非循环”版本时:/我想我会坚持本地范围。 – 2011-06-13 08:30:05