WPF memory leak

前端 未结 6 1960
生来不讨喜
生来不讨喜 2021-01-13 00:35

I have a simple wpf application. In main window i have stack panel and 2 buttons. First button adds 100 my user controls (without any data bindings, events, bitmaps), and se

6条回答
  •  臣服心动
    2021-01-13 01:32

    I want to release memory immediately.

    Don't. Trust GC.

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    

    Don't. Trust GC.

    After 5 - 10 min memory releases

    Didn't I say trust GC?


    • Garbage collection model will make sure the unwanted managed memory in your system is released (which includes almost all of your controls memory). It uses an algorithm for optimising which includes generations, free memory available, possibly CPU available... so GC.Collect() will interfere with it.

    • GC.Collect() is asynchronous so no immediate effect.

    • The only resource you need to be careful is the unmanaged resource which usually is handled by Dispose Pattern. Otherwise don't mess with GC, it does its job very well.

提交回复
热议问题