1

I want to observe garbage collection through finalizer/destructon on C#, VS(Mac OS):

I am trying to observe garbage collection on C# using finalizer/destructor in C# OOP. I did create the object correctly in Main but it seems that Mac OS version of visual studio does not seem to accept my Debug.Write query (same query seems to show debug message on windows OS version of it).

I have looked through information regarding garbage collection on C# but it seems that there isn't much information on C# garbage collection on Mac version of Visual Studio so I am wondering if anyone could hint me on it;

For a quick overview of my "short" program:

In class Members I have the following attached at the end:

~Members()
        {
            Console.WriteLine("Deconstruction of Members object");
            Debug.Write("Destruction of Members object");
        }

In Main of class Program:

static void Main(string[] args)
        {
            Members member1 = new Members();
            member1.Introducing(true);
        }

Console Output (on VS(Mac.ver)):

Object Created
My salary is 60000

Press any key to continue...flogout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]
  • "Object Created" is a WriteLine command I stuck in the constructor to see if the obj is created;
  • "My salary is 60000" is the Introducing() method I called in main

Application Output

"You may only use the Microsoft .NET Core Debugger (vsdbg) with
Visual Studio Code, Visual Studio or Visual Studio for Mac software
to help you develop and test your applications.
-------------------------------------------------------------------
Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.1.9/System.Private.CoreLib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Load/Projects/IntroOOP/IntroOOP/bin/Debug/netcoreapp2.1/IntroOOP.dll'. Symbols loaded.
Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.1.9/System.Runtime.dll'. Module was built without symbols.
Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.1.9/System.Console.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.1.9/System.Threading.dll'. Module was built without symbols.
Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.1.9/System.Runtime.Extensions.dll'. Module was built without symbols.
Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.1.9/System.Collections.dll'. Module was built without symbols.
Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.1.9/System.Runtime.InteropServices.dll'. Module was built without symbols.
The program '[17396] IntroOOP.dll' has exited with code 0 (0x0)."
  • Application Output does not seem to contain my Debug message
  • I expected to see what I've put in Debug.Write("") but it seems to be nowhere.
  • I did use Debug using System Diagnostics
  • expected output: "Destruction of Members object"

Observing the last parts of the console from "Press any key to continue" where "flogout" and on appears when I press a key: I could only think that VS(MacOS) reacts differently but I would like to know about this more.

If it is not the case that garbage collector on C# is automated on VS(MacOS):

How could I observe the destructor behavior?

  • there is only single finalizer in this simple few lines of code
  • in my code, I believe that I pass all the exceptions for not having a finalizer run based on ("Are .net finalizers always executed?")

A finalizer may not run, for example, if:

  • Another finalizer throws an exception.
  • Another finalizer takes more than 2 seconds.
  • All finalizers together take more than 40 seconds.
  • An AppDomain crashes or is unloaded (though you can circumvent this with a critical finalizer (CriticalFinalizerObject, SafeHandle or
    something like that)
  • No garbage collection occurs
  • The process crashes

-OregonGhost

------------------------Update Note on this Question------------------------

I really do not think that this is a duplicate question of "Are .net finalizers always executed?" because I was trying to set up an environment where finalizer is executed;

However comment by Caius Jard seems to be a good enough answer for now:

"Your programm will need to run for long enormous go that the garbage collector activates. As it is your app is shutting down soon and the OS is clearing up all its resources, the app garbage collector is never needing to run" -Caius Jard

I guess I was foolish to draw towards an idea that garbage collection is to be automated on a specific language(C#) because an OS(MAC) & IDE(Visual Studio but this is Mac version) change here.

What I will take away from the current comment is that:

  • simply, VS(MacOS) reacts differently from VS(Windows) (unsure which difference affects which in the systems
  • And as Caius Jard said, this simple program of a few lines of code may be shutting down too quickly

I also read through What is the difference between Visual Studio on Mac and VS on Windows? but I am not sure what to exactly take away from such article, not having a strong computer engineering/computer science background.

At this point, I hope to learn to execute the finalizer in VS(MacOS) somehow.

And if there is such specific way, I would love to know why the program of mine does not execute the Debug command in the VS(MacOS) and how the newly suggested method bypasses such behavior of VS(MacOS)

Personally, I hope to see an already-answered duplicate question, so I could quickly take away from such solution but I don't seem to be the master of browsing.

References:

  1. Are .net finalizers always executed?
  2. https://www.quora.com/What-is-the-difference-between-Visual-Studio-on-Mac-and-VS-on-Windows
soohyeok
  • 148
  • 7
  • 2
    Your programmwill need to run for long enormous go that the garbage collector activates. As it is your app is shutting down soon and the OS is clearing up all its resources, the app garbage collector is never needing to run – Caius Jard Jun 12 '19 at 02:07

0 Answers0