垃圾收集不会减少当前内存使用量 - 在释放模式下.为什么?

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.

这篇关于垃圾收集不会减少当前内存使用量 - 在释放模式下.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)