class GCTest {
static Object r1;
static void Main() {
r1 = new Object();
Object r2 = new Object();
Object r3 = new Object();
System.GC.Collect(); // what can be reclaimed here ?
r1 = null;
r3.ToString();
System.GC.Collect(); // what can be reclaimed here ?
}
}
// code from - DonBox's Essential .Net
At the first line, there are no further references to r2, so this object could be reclaimed at this point. At the last line, the object created for r1 no longer has a reference, and the local variables r2 and r3 are finished with, so all three objects could be reclaimed at this point. However, this will depend on how it is all compiled.
The three are all root references, as nothing else refers to them. Had r1 been an instance field rather than a static one, then a reference to your GCTest class would have made the r1 reference not a root reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With