Garbage Collection does not reduce current memory usage - in release mode. Why?(垃圾收集不会减少当前内存使用量 - 在释放模式下.为什么?)
问题描述
我构建了一个快速程序,该程序需要遍历一个巨大的日志文件(几百万条记录)并从内部查找各种零碎的内容.由于数据量如此之大,我一直好奇地查看我的 Windows 任务管理器性能选项卡,看看正在使用多少 CPU 和内存.
I built a quick program that needed to loop through an enormous log file (a couple of million records) and find various bits and pieces from inside. Because the volume of data was so huge, I have been curious to watch my Windows Task Manager performance tab and see how much CPU and memory is being used.
程序成功获得我的结果后,CPU 使用率明显下降.但是在执行过程中看到我的内存使用量慢慢上升到几 GB 后,它保持不变.
After the program successfully gets my results, the CPU usage goes right down, obviously. But after watching my memory usage slowly go up to several gigabytes during execution, it stays the same.
我尝试在函数结束时调用 GC.Collect(),尝试将内容设置为 null,尝试在 Release 模式下运行程序(我听说 GC.Collect() 可能无法按我的意愿工作进入调试模式).
I have tried to call GC.Collect() at the end of my function, tried setting things to null, tried to run the program in Release mode (I heard that GC.Collect() might not work as I want it to in debug mode).
如果我关闭程序,内存使用量会立即下降,但我无法弄清楚为什么我无法在应用程序生命周期内清理我的应用程序.归根结底,这是一个一次性的应用程序,但我只是想知道我缺少什么,即所有这些记忆都保留着什么.
The memory usage goes right down if I close the program, but I can't figure out why I can't clean my app up during the apps lifetime. At the end of the day this is a throwaway app, but I'm just curious to know what I'm missing i.e. what is holding on to all this memory.
想法?
推荐答案
当你调用 Collect() 时,垃圾收集器不保证运行.它只是标记要收集的对象.下次 GC 运行时,它将收集"标记的对象.
The garbage collector is not guaranteed to run when you call Collect(). It simply flags the object for collection. The next time the GC runs it will "collect" the flagged object.
.NET 中无法强制 GC 在特定时间点收集 - 如果您需要此功能,则需要使用本机代码.
There is no way in .NET to force the GC to collect at a specific point in time - if you need this functionality you'll need to go to native code.
这篇关于垃圾收集不会减少当前内存使用量 - 在释放模式下.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:垃圾收集不会减少当前内存使用量 - 在释放模式下.为什么?


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01