c# - 观察垃圾回收(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]
- “Object Created”是我在构造函数中卡住的WriteLine命令,看是否创建了obj;
- “我的工资是 60000”是我在 main 中调用的 Introducing() 方法
应用程序输出
"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)."
- 应用程序输出似乎不包含我的调试消息
- 我希望看到我在 Debug.Write("") 中放入的内容,但它似乎无处可去。
- 我确实使用系统诊断使用调试
- 预期输出:“成员对象的破坏”
从“按任意键继续”观察控制台的最后部分,当我按下一个键时出现“flogout”和on:我只能认为VS(MacOS)的反应不同,但我想了解更多。
如果不是 C# 上的垃圾收集器在 VS(MacOS) 上自动化:
我怎么能观察到析构函数的行为?
- 在这几行简单的代码中只有一个终结器
- 在我的代码中,我相信我通过了所有异常,因为没有基于(“.net 终结器总是执行?”)运行终结器
终结器可能不会运行,例如,如果:
- 另一个终结器抛出异常。
- 另一个终结器需要 2 秒以上。
- 所有终结器加起来需要 40 多秒。
- AppDomain 崩溃或被卸载(尽管您可以使用关键终结器(CriticalFinalizerObject、SafeHandle 或
类似的东西)规避这个问题)- 没有垃圾收集发生
- 进程崩溃
------------------------------------关于这个问题的更新说明---------- ---
我真的不认为这是 “.net 终结器总是执行吗?”的重复问题。因为我试图建立一个执行终结器的环境;
然而,Caius Jard的评论目前似乎是一个足够好的答案:
“您的程序将需要长时间运行垃圾收集器激活的大量时间。由于您的应用程序即将关闭并且操作系统正在清理其所有资源,因此应用程序垃圾收集器永远不需要运行” - Caius Jard
我想我愚蠢地认为垃圾收集将在特定语言(C#)上自动化,因为 OS(MAC)和 IDE(Visual Studio,但这是 Mac 版本)在这里发生了变化。
我将从当前评论中得出的结论是:
- 简单地说,VS(MacOS) 与 VS(Windows) 的反应不同(不确定哪个差异会影响系统中的哪个
- 正如 Caius Jard 所说,这个由几行代码组成的简单程序可能关闭得太快了
我还阅读了 Mac 上的 Visual Studio 和 Windows 上的 VS 有什么区别?但我不确定从此类文章中究竟能带走什么,因为我没有强大的计算机工程/计算机科学背景。
在这一点上,我希望学会以某种方式在 VS(MacOS) 中执行终结器。
如果有这样的具体方法,我很想知道为什么我的程序不执行VS(MacOS)中的Debug命令以及新建议的方法如何绕过VS(MacOS)的这种行为
就个人而言,我希望看到一个已经回答过的重复问题,所以我可以很快摆脱这种解决方案,但我似乎不是浏览大师。
参考:
解决方案
推荐阅读
- c# - 发布到 IIS 时无法从 DotNetCore 调用 .NET 4.6.1 项目
- kubernetes - RoleBinding 和 ClusterRoleBinding 中的 Kubernetes RBAC apiGroup 字段
- azure - 如何使用 powershell 获取 azure vm 的负载均衡器名称?
- css - 为什么父 DIV 会调整大小,而子元素仍有向下移动的空间?
- selenium - 通过 selenium 运行 chrome
- clips - Clips:对要搜索的向量变量的限制
- windows - 某些应用程序(Eclipse、Notepad++、MSSQL)中的汉字大小不同
- javascript - 在 iframe 中使用时是否会影响选项卡行为?
- javascript - 来自对象的 Javascript / React 列表标题
- typescript - 带解构的 TypeScript Array.map 的 Rust 等价物是什么?