question

VaskaranSarcar-9178 avatar image
0 Votes"
VaskaranSarcar-9178 asked DanielZhang-MSFT commented

Why do I see a different GC behavior in .NET5 and .NET Framework(4.7.2) on Windows 10?

Hi,
Consider the following program:
class Program
{
class Sample
{
~Sample()
{
System.Diagnostics.Trace.WriteLine("Destructor is called..");
}
}
static void Main(string[] args)
{

         System.Diagnostics.Trace.WriteLine("Experimenting destructors.");

         Sample sample = new Sample();
         sample = null;
            
         System.Diagnostics.Trace.WriteLine("GC is about to start.");
         GC.Collect();
         GC.WaitForPendingFinalizers();
            
         System.Diagnostics.Trace.WriteLine("GC is completed.");

         Console.ReadKey();
     }
 }

In .NET Framework 4.7.2, I can see the destructor message clearly[In fact, I do not need to call the GC there]. But the same program in .NET 5(or .NET 6 or .NET Core 3.1), does not show the destructor message.

Additional info:
I have checked that .NET 5/6 shows the destructor message you compose the object into another object and the composed object is not inside the scope of Main().

I'd appreciate your thoughts on this.

Regards,
Vaskaran

dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

To remove any further confusion, I copy my message here also:

I am NOT talking about automatic GC activity;instead,I am asking it from a different perspective:
I force GC to collect the garbages before the program ends. When I do this, in the .NET Framework , I see the following output:
Experimenting destructors.
GC is about to start.
Destructor is called.
GC is completed.
If you do not invoke the GC in this way, based on the "timing" issues, you may see the output in a different order. But I repeat, since I force GC to collect garbages, you see the message from the destructor in the 3rd line of the output in the .NET Framework. And it is a stable output. But in .NET Core/.NET 5/6,
the 3rd line of output is absent at all. But as a programmer, I'd like to know why this is happening.
So, it's NOT the question: when finalizer calls are taking place? Instead, the question is: Why do the same program produce steady but different output?

0 Votes 0 ·
lextm avatar image lextm VaskaranSarcar-9178 ·

Probably only https://github.com/dotnet/runtime/issues can give you an answer. But the CLR implementations on .NET Framework and .NET Core are different by design, and Microsoft has no intention to unify them (as breaking things on .NET Framework is rather terrible), so you can observe many other differences as well.

0 Votes 0 ·

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

Hi VaskaranSarcar-9178,
As document said that in .NET Framework applications (but not in .NET Core applications), finalizers are also called when the program exits.
So .NET Core does not run finalizers at the end of the program.
And madreflection has provided a detailed explanation in this thread you can refer to.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Daniel,
Thanks for your reply. If you kindly notice it again, you'll understand that: I am NOT talking about automatic GC activity;instead,I am asking it from a different perspective:
I force GC to collect the garbages before the program ends. When I do this, in the .NET Framework , I see the following output:
Experimenting destructors.
GC is about to start.
Destructor is called.
GC is completed.
If you do not invoke the GC in this way, based on the "timing" issues, you may see the output in a different order. But I repeat, since I force GC to collect garbages, you see the message from the destructor in the 3rd line of the output in the .NET Framework. And it is a stable output. But in .NET Core/.NET 5/6,
the 3rd line of output is absent at all. But as a programmer, I'd like to know why this is happening.
So, it's NOT the question: when finalizer calls are taking place? Instead, the question is: Why do the same program produce steady but different output?

0 Votes 0 ·

Hi @VaskaranSarcar-9178,
I saw that Maoni0 said it's the lifetime tracking in JIT in this thread.
Best Regards,
Daniel Zhang


0 Votes 0 ·