I'm confused why this function shows memory constantly increasing
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 ^CPress any key to continue . . .
[<EntryPoint>]
let main _ =
startRead1() |> Async.RunSynchronously
0
Whereas this one shows stable memory
let rec startRead2() =
async {
while true do
do! Async.Sleep 1
System.GC.Collect()
let mem = System.GC.GetTotalMemory(true)
printfn "%d" mem
}
The synchronous version is stable too.
let rec startRead3() =
System.Threading.Thread.Sleep 1
System.GC.Collect()
let mem = System.GC.GetTotalMemory(true)
printfn "%d" mem
startRead3()
I'm running in release mode without debugger attached, FS 3.1, .NET 4.5.1.
Quoted from here (end of article) :
"Also, F# async has its problems too (the most common gotcha is that tail-recursive functions must use
return!
instead ofdo!
to avoid leaks)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With