首页 > 解决方案 > 观察垃圾回收(C#、MacOS、Visual Studio)(终结器、析构器)

问题描述

我想通过 C#、VS(Mac OS) 上的终结器/析构函数观察垃圾收集:

我正在尝试使用 C# OOP 中的终结器/析构函数观察 C# 上的垃圾收集。我确实在 Main 中正确创建了对象,但似乎 Mac OS 版本的 Visual Studio 似乎不接受我的 Debug.Write 查询(相同的查询似乎在它的 Windows OS 版本上显示调试消息)。

我查看了有关 C# 垃圾收集的信息,但似乎没有太多关于 Mac 版 Visual Studio 上的 C# 垃圾收集的信息,所以我想知道是否有人可以提示我;

快速浏览我的“短”程序:

在班级成员中,最后附有以下内容:

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

在主要课程中:

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

控制台输出(在 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]

应用程序输出

"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)."

从“按任意键继续”观察控制台的最后部分,当我按下一个键时出现“flogout”和on:我只能认为VS(MacOS)的反应不同,但我想了解更多。

如果不是 C# 上的垃圾收集器在 VS(MacOS) 上自动化:

我怎么能观察到析构函数的行为?

终结器可能不会运行,例如,如果:

  • 另一个终结器抛出异常。
  • 另一个终结器需要 2 秒以上。
  • 所有终结器加起来需要 40 多秒。
  • AppDomain 崩溃或被卸载(尽管您可以使用关键终结器(CriticalFinalizerObject、SafeHandle 或
    类似的东西)规避这个问题)
  • 没有垃圾收集发生
  • 进程崩溃

-俄勒冈幽灵

------------------------------------关于这个问题的更新说明---------- ---

我真的不认为这是 “.net 终结器总是执行吗?”的重复问题。因为我试图建立一个执行终结器的环境;

然而,Caius Jard的评论目前似乎是一个足够好的答案:

“您的程序将需要长时间运行垃圾收集器激活的大量时间。由于您的应用程序即将关闭并且操作系统正在清理其所有资源,因此应用程序垃圾收集器永远不需要运行” - Caius Jard

我想我愚蠢地认为垃圾收集将在特定语言(C#)上自动化,因为 OS(MAC)和 IDE(Visual Studio,但这是 Mac 版本)在这里发生了变化。

我将从当前评论中得出的结论是:

我还阅读了 Mac 上的 Visual Studio 和 Windows 上的 VS 有什么区别?但我不确定从此类文章中究竟能带走什么,因为我没有强大的计算机工程/计算机科学背景。

在这一点上,我希望学会以某种方式在 VS(MacOS) 中执行终结器。

如果有这样的具体方法,我很想知道为什么我的程序不执行VS(MacOS)中的Debug命令以及新建议的方法如何绕过VS(MacOS)的这种行为

就个人而言,我希望看到一个已经回答过的重复问题,所以我可以很快摆脱这种解决方案,但我似乎不是浏览大师。

参考:

  1. .net 终结器是否总是执行?
  2. https://www.quora.com/What-is-the-difference-between-Visual-Studio-on-Mac-and-VS-on-Windows

标签: c#macosgarbage-collectionvisual-studio-macfinalizer

解决方案


推荐阅读