C# - Garbage Collection

前端 未结 9 825
闹比i
闹比i 2021-01-31 11:15

Ok so I understand about the stack and the heap (values live on the Stack, references on the Heap).

When I declare a new instance of a Class, this lives on the heap, wit

9条回答
  •  日久生厌
    2021-01-31 11:49

    Can I do my own? If so is there any real benefit to doing this myself or should I just leave it.

    Yes you can with GC.Collect but you shouldn't. The GC is optimized for variables that are short lived, ones in a method, and variables that are long lived, ones that generally stick around for the life time of the application.

    Variables that are in-between aren't as common and aren't really optimum for the GC.

    By forcing a GC.Collect you're more likely to cause variables in scope to be in forced into that in-between state which is the opposite from you are trying to accomplish.

    Also from the MSDN article Writing High-Performance Managed Applications : A Primer

    The GC is self-tuning and will adjust itself according to applications memory requirements. In most cases programmatically invoking a GC will hinder that tuning. "Helping" the GC by calling GC.Collect will more than likely not improve your applications performance

提交回复
热议问题