如何使.NET取消提交未使用的RAM? [英] How to get .NET to uncommit unused RAM?

查看:66
本文介绍了如何使.NET取消提交未使用的RAM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的程序的统计信息,该程序非常密集地使用内存,在峰值时消耗了6 GB,但随后将所有内容保存到磁盘上,并且范围很小:

Here are the stats for my program after it used memory extremely intensively, consuming 6 GB at its peak, but then saving everything to disk and leaving very little in scope:

观察到几乎所有内容都超出了范围并被垃圾回收-堆的大小很小.但是,.NET保留了181 MB的 committed .

Observe that almost everything has gone out of scope and has been garbage collected - the heap sizes are tiny. And yet, .NET keeps 181 MB committed.

我不介意保留字节,因为这只占用地址空间.但是提交的内存很烦人-即使它仅驻留在页面文件中,也仍然很多.

I don't mind the reserved bytes, since that only consumes address space. But the committed memory is annoying – even if it only resides in the page file, it's still quite a lot.

推荐答案

根据 CLR由内而外-发现大对象堆 CLR在第2代垃圾收集期间取消使用未使用的提交内存.

According to CLR Inside Out - Large Object Heap Uncovered the CLR decommits un-used committed memory during a Gen 2 garbage collection.

这意味着您既可以等待第二代垃圾收集独立发生,也可以使用GC.Collect()强制垃圾收集-您确实需要知道自己在做什么,如果您选择但是,这条路线会与垃圾收集器的标准垃圾收集周期发生混乱,而这实际上会影响性能:

This means that you can either wait for a Gen 2 garbage collection to happen on its own or you can force one using GC.Collect() - you really need to know what you are doing if you choose this route however as it messes with the garbage collectors standard garbage collection cycles which can really mess with performance:

  • 完整的垃圾收集很慢,所以您真的不想经常这样做
  • 比标准时间表更频繁地触发垃圾回收将把更多对象提升到更高的世代,这意味着它们可能不会像通常那样尽快被回收

据我所知(根据我相当有限的研究),CLR在其他情况下不会释放已提交的内存.

As far as I am aware (from my fairly limited research) the CLR won't release committed memory in other situations.

您还应该考虑这是否确实是一个问题:

You should also consider whether or not this is actually a problem:

  • 如果您的进程将立即继续执行其他占用大量内存的处理,那么它将很快又需要该内存,因此无论如何都不要使用该内存并没有太多好处
  • 如果处理完成并且将很快终止,则无论如何将取消使用内存
  • 无论如何都要提交这么多的内存并不一定会有很大的缺点-是的,这意味着该内存已分配了后备存储,但是,如果系统处于内存压力下,则后备存储很可能是页面文件.

更新:高级.NET调试:托管堆和垃圾回收似乎支持以下假设:仅在第2代/完全回收期间不分配内存:

Update: Advanced .NET Debugging: Managed Heap and Garbage Collection appears to back up the hypothesis that memory is only uncommitted during a gen 2 / full collection:

在收集第2代中的对象时,CLR堆管理器将废弃这些段中的内存,而当不再需要某个段时,它将被完全释放.

When objects in generation 2 are collected, the CLR heap manager decommits memory in the segments, and when a segment is no longer needed, it is entirely freed.

这篇关于如何使.NET取消提交未使用的RAM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆