我构建了一个快速程序,该程序需要遍历一个巨大的日志文件(几百万条记录)并从内部查找各种零碎的内容.由于数据量如此之大,我一直好奇地查看我的 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.
这篇关于垃圾收集不会减少当前内存使用量 - 在释放模式下.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!