简体   繁体   English

F#异步递归调用中的内存泄漏

[英]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 . 25676 36760 36840 36884 36928 37972 37016 37060 37456 37456 37500 37544 37588 37632 37676 37676 37720 37764 37808 37852 37378 37896 37896 37940 37984 38984 38072 38072 38116 38160 38204 38248 38292 38336 38380 38380 38424 38468 38512 38556 38600 38644 38688 38732 38886 38864 38952 38996 39040 39084 39128 39172 39216 ^ C按任意键继续。 . .

[<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. 我正在发布模式下运行,没有连接调试器,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 of do! to avoid leaks) " 而且,F#异步也有问题(最常见的陷阱是尾递归函数必须使用return!而不是do!以避免泄漏)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM