Linked Questions
140 questions linked to/from Why is an unreferenced object not finalized immediately after a call to `GC.Collect`?
18 votes
4 answers
18k views
How does garbage collection and scoping work in C#? [duplicate]
I'm learning C# coming from python and wish to know how the C# garbage collector works - I found that I understood a lot more about python once I figured out what it was doing behind the scenes, and ...
5 votes
3 answers
3k views
When is an object eligible for garbage collection? [duplicate]
In the book C# 5.0 in a Nutshell by Joseph Albahari I found this: using System; using System.Text; class Test { static void Main() { StringBuilder ref1 = new StringBuilder ("object"...
9 votes
1 answer
1k views
Why call GC.KeepAlive in the end, and not in the beginning?
From GC.KeepAlive() on MSDN: Code this method at the end, not the beginning, of the range of instructions where obj must be available. Why does it have such non-intuitive behavior?
5 votes
1 answer
5k views
C# GC not freeing memory [duplicate]
I'm having an awful time with C# not freeing up memory for a large structure I'm holding in memory after I'm no longer referencing it. I've included some code below that showcases a similar problem ...
1 vote
2 answers
5k views
How to Dispose a list of objects;Does it Free the Memory [duplicate]
I have the following class. public class Foo { private string _DirName; private FileStream _MyfileStream; public FileStream MyfileStream { get { return _MyfileStream; } ...
7 votes
1 answer
2k views
What exactly happens when I ask dotMemory to force garbage collection [duplicate]
I use dotMemory to profile my application and I noticed the below behavior: inside my code there are some points where I perform garbage collection manually by using GC.Collect(); GC....
-1 votes
1 answer
1k views
c# : why GC can't collect Weakreference's target in my code? [duplicate]
I wrote this code: dog joe = new dog(); WeakReference wr = new WeakReference(joe); if (wr.IsAlive) { Console.WriteLine("Yes,first time")...
8 votes
2 answers
207 views
Garbage collection - One works but not the other, how come? [duplicate]
So I have this simply Bell class that I'm testing garbage collection on: public class Bell { public void Ring() { Console.WriteLine("Ding ding"); } } If I run this code segment ...
2 votes
2 answers
2k views
C# destructor not called even after code scope + GC? [duplicate]
I've got a simple test code in Nunit+.net core 2.0 under VS2019: public class Tests { public static int i = 0; class First { ~First() { i += 1; ...
2 votes
1 answer
2k views
.NET Garbage Collector Basics [duplicate]
I apologize if the answer to this question is trivial. But I still cannot figure out this by myself. How does the garbage collector in .NET identify what objects on the heap are garbage and what ...
1 vote
1 answer
1k views
finalizer not called after GC.Collect() [duplicate]
I think I am missing something fundamental and hope you can help. Below code creates an object, removes the reference and call the garbage collector. My expectation was that SomeClass's finalizer ...
4 votes
1 answer
452 views
Why Debug is different to Release in this specific and very simple case? [duplicate]
... and by different I don't mean performance, debug-ability candies and so on, I am meaning different programs (programs that for the same input gives different outputs). Take the following program: ...
-2 votes
2 answers
2k views
c# correct way to call GC.Collect [duplicate]
I have an application that do complex processing and creates too many objects, I want to free up the memory after the processing is complete. I'm currently calling GC.collect in my application in a ...
0 votes
1 answer
600 views
Why does my program's memory usage rise quickly and then go flat? [duplicate]
Hi everyone I'm making a C# WinForms application that searches for duplicate images in a directory. It starts by calling a constructor with every image in the directory. There is a lot of files in ...
0 votes
1 answer
122 views
How does the .net GC handles local objects passed into async callbacks [duplicate]
How does the GC determines that it needs to deference a local object passed into a callback? See the comments below on the need of disposing an object that is no longer referenced in an async ...