F# Fabulous/Xamarin 内存管理,为什么我不能使用(自动处理)这个流?

F# Fabulous/Xamarin memory management, why can't I use (auto dispose) this stream?

我是getting started with Fabulous,我已经创建了标准模板并修改它以显示位图。

我正在使用提供的代码 here 生成位图流,并且我正在这样定义我的视图

let view (model: Model) dispatch =
    let strm = Bmp.Create 1000 1000 (fun row col ->
        let red = float row / float 1000
        let blue = float col / float 1000
        Color.FromRgb(red, 0.0, blue)
    )
    let img = ImageSource.FromStream(fun _ -> strm)
    View.ContentPage(
      content = View.Image(source = img)
    )

这似乎工作得很好,但如果我将 let strm 更改为 use strm,它就不再工作了。为什么?

这里的内存是如何管理的,如果我 use 流以便它一超出范围就被处理掉,为什么它不起作用?在这种情况下,let 是否存在任何内存泄漏问题?

if I use the stream so that it gets disposed as soon as it goes out of scope?

是的,完全正确。

Are there any memory leak issues with let in this case?

不用担心,GC会为您处理。

现在的问题是:如果GC可以处理所有这些情况,为什么我们还需要auto dispose

首先,对于大资源,最好在我们不再使用它们时立即处理它们,而不是依赖 GC 稍后处理资源。

其次——更重要的是——对于共享资源,我们一定要在不用的时候尽快销毁,以便其他人可以访问这些资源。