Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak in F# async recursive call

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.

like image 847
lobsterism Avatar asked Nov 02 '15 15:11

lobsterism


1 Answers

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 of do! to avoid leaks)"

like image 118
Sehnsucht Avatar answered Sep 22 '22 16:09

Sehnsucht