F#异步递归调用中的内存泄漏
Memory leak in F# async recursive call
我很困惑为什么这个函数显示内存不断增加
let rec startRead1() =
async {
do! Async.Sleep 1
System.GC.Collect()
let mem = System.GC.GetTotalMemory(true)
printfn "%d" mem
do! startRead1()
}
25676
36760
36840
36884
36928
36972
37016
37060
37104
37148
37192
37236
37280
37324
37368
37412
37456
37500
37544
37588
37632
37676
37720
37764
37808
37852
37896
37940
37984
38028
38072
38116
38160
38204
38248
38292
38336
38380
38424
38468
38512
38556
38600
38644
38688
38732
38776
38820
38864
38908
38952
38996
39040
39084
39128
39172
39216
^C按任意键继续。 . .
[<EntryPoint>]
let main _ =
startRead1() |> Async.RunSynchronously
0
而这个显示稳定的内存
let rec startRead2() =
async {
while true do
do! Async.Sleep 1
System.GC.Collect()
let mem = System.GC.GetTotalMemory(true)
printfn "%d" mem
}
同步版本也很稳定
let rec startRead3() =
System.Threading.Thread.Sleep 1
System.GC.Collect()
let mem = System.GC.GetTotalMemory(true)
printfn "%d" mem
startRead3()
我 运行 处于未附加调试器的发布模式,FS 3.1,.NET 4.5.1。
引自here(文末):
"Also, F# async has its problems too (the most common gotcha is that tail-recursive functions must use return!
instead of do!
to avoid leaks)"
我很困惑为什么这个函数显示内存不断增加
let rec startRead1() =
async {
do! Async.Sleep 1
System.GC.Collect()
let mem = System.GC.GetTotalMemory(true)
printfn "%d" mem
do! startRead1()
}
25676 36760 36840 36884 36928 36972 37016 37060 37104 37148 37192 37236 37280 37324 37368 37412 37456 37500 37544 37588 37632 37676 37720 37764 37808 37852 37896 37940 37984 38028 38072 38116 38160 38204 38248 38292 38336 38380 38424 38468 38512 38556 38600 38644 38688 38732 38776 38820 38864 38908 38952 38996 39040 39084 39128 39172 39216 ^C按任意键继续。 . .
[<EntryPoint>]
let main _ =
startRead1() |> Async.RunSynchronously
0
而这个显示稳定的内存
let rec startRead2() =
async {
while true do
do! Async.Sleep 1
System.GC.Collect()
let mem = System.GC.GetTotalMemory(true)
printfn "%d" mem
}
同步版本也很稳定
let rec startRead3() =
System.Threading.Thread.Sleep 1
System.GC.Collect()
let mem = System.GC.GetTotalMemory(true)
printfn "%d" mem
startRead3()
我 运行 处于未附加调试器的发布模式,FS 3.1,.NET 4.5.1。
引自here(文末):
"Also, F# async has its problems too (the most common gotcha is that tail-recursive functions must use
return!
instead ofdo!
to avoid leaks)"