垃圾收集不会减少当前的内存使用情况 - 在释放模式。为什么?内存、垃圾、模式、情况

2023-09-02 10:53:29 作者:用一生,换你一次初相遇ゝ

我建一个需要遍历一个巨大的日志文件(一个几百万条记录),并寻找各种零碎从内快速程序。由于数据量如此之大,我一直好奇地看着我的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使用率降权,效果显着。但看我的内存使用情况后,慢慢上升到数千兆字节执行过程中,它保持不变。

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,试图在释放模式运行这个程序(我听说,所以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.

思考?

推荐答案

在垃圾收集器不能保证当你调用收集运行()。它只是标记为收藏的对象。下一次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收集在一个特定时间点 - 如果你需要这个功能,你需要去本地code。

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.