Testing/Verifying a WeakReference

前端 未结 3 1942
逝去的感伤
逝去的感伤 2021-02-12 22:29

I\'d like to verify that code setting up a WeakReference does not accidentally hold a strong reference to the referenced object. (Here\'s an example of how it is easy to acciden

3条回答
  •  执笔经年
    2021-02-12 22:59

    I got in touch with Microsoft about this and learned/confirmed that:

    • GC.Collect() forces a blocking garbage collection.
    • When GC.Collect() runs, it won't mysteriously skip over collection-eligible objects. Predictable rules are followed for determining which objects to collect. As long as you operate with an understanding of those rules (i.e. how finalizable objects are handled), you can force a particular object to be destroyed though the memory used by the destroyed object may or may not be freed.

    More information on my blog: Can .Net garbage collection be forced?

提交回复
热议问题