C# - Garbage Collection

前端 未结 9 822
闹比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:42

    First off, to Erics seminal post about The truth about value types

    Secondly on Garbage collection, the collector knows far more about your running program than you do, don't try to second guess it unless you're in the incredibly unlikely situation that you have a memory leak.

    So to your second question, no don't try to "help" the GC.

    I'll find a post to this effect on the CG and update this answer.

    0 讨论(0)
  • 2021-01-31 11:48

    Your understanding of Garbage Collection is good enough. Essentially, an unreferenced instance is deemed as being out-of-scope and no longer needed. Having determined this, the collector will remove an unreferenced object at some future point.

    There's no way to force the Garbage Collector to collect just a specific instance. You can ask it to do its normal "collect everything possible" operation GC.Collect(), but you shouldn't.; the garbage-collector is efficient and effective if you just leave it to its own devices.

    In particular it excels at collecting objects which have a short lifespan, just like those that are created as temporary objects. You shouldn't have to worry about creating loads of objects in a loop, unless they have a long lifespan that prevents immediate collection.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-31 11:50

    If you are interested in performance of some areas in your code when writing C#, you can write unsafe code. You will have a plus of performance, and also, in your fixed block, the garbage collector most likely will not occur.

    0 讨论(0)
  • 2021-01-31 11:51

    Read the following article by Microsoft to get a level of knowledge about Garbage Collection in C#. I'm sure it'll help anyone who need information regarding this matter.

    Memory Management and Garbage Collection in the .NET Framework

    0 讨论(0)
  • 2021-01-31 11:51

    Garbage collection is basically reference tracking. I can't think of any good reason why you would want to change it. Are you having some sort of problem where you find that memory isn't being freed? Or maybe you are looking for the dispose pattern

    Edit: Replaced "reference counting" with "reference tracking" to not be confused with the Increment/Decrement Counter on object Reference/Dereference (eg from Python). I thought it was pretty common to refer to the object graph generation as "Counting" like in this answer: Why no Reference Counting + Garbage Collection in C#? But I will not pick up the glove of (the) Eric Lippert :)

    0 讨论(0)
提交回复
热议问题