大根0堆在.NET中,浪费内存? [英] Large Gen 0 Heap in .NET, wasting memory?

查看:284
本文介绍了大根0堆在.NET中,浪费内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用大量内存的ASP.NET / C#的Web应用程序。

I have an ASP.NET / C# web application that is using a lot of memory.

蚂蚁内存分析器和性能监视器都显示的Application_Start在我的第0级堆迅速增长至约1 GB的大小。我读这里的性能监视器计数器0代堆实际上是显示了预算的创0,而不是大小(这是我采取的意思是不是所有的内存是私人工作设置过程中的一部分吗?)。但是蚂蚁探查器显示大约700 MB的分配到.NET未使用的内存,而这个的确实的似乎是进程的专用工作集(如报告taskmgr)的一部分。我猜这大量的未使用的内存是关系到大将军0堆。

ANTS Memory Profiler and PerfMon both show my Gen 0 heap growing rapidly to about 1 GB in size during Application_Start. I read here that the PerfMon counter for Gen 0 Heap is actually showing the "budget" for Gen 0, not the size (which I take to mean not all that memory is part of the private working set of the process?). However the ANTS profiler does show about 700 MB of "unused memory allocated to .NET", and this does seem to be part of the private working set of the process (as reported in taskmgr). I am guessing this large amount of unused memory is related to the large Gen 0 heap.

什么是发生在的Application_Start,而这种情况是,我在一个while循环念叨从一个SqlDataReader一百万行。这些被用来填充一个大的缓存以备后用。鉴于此,明显的罪魁祸首的大量未使用的内存是大对象堆碎片,但我不认为这是因为我pre-分配不止是需要我的大缓存对象的情况。要确定,我甚至尝试注释掉,实际上增加了我的缓存对象循环的一部分;它在分配未使用的内存量并没有区别。

What is happening in Application_Start while this happens is that I'm in a while loop reading about a million rows from a SqlDataReader. These are being used to populate a large cache for later use. Given this, the obvious culprit for the large amount of unused memory was large object heap fragmentation, but I don't think this is the case as I'm pre-allocating more than is needed for my large cache object. To be certain, I even tried commenting out the part of the loop that actually adds to my cache object; it made no difference in the amount of unused memory allocated.

作为测试,我试着经常迫使根0的垃圾回收循环期间(反对所有的建议,我知道),这引起了gen0堆的大小停留下来约128 MB,也导致只有少数MB的未使用的可用内存。但它也刷爆了我的CPU,并提出的Application_Start走的时间太长了。

As a test, I tried frequently forcing garbage collection of gen 0 during the loop (against all recommendations, I know), and this caused the size of the gen0 heap to stay down around 128 MB and also resulted in only a few MB of unused free memory. But it also maxed out my CPU and made Application_Start take way too long.

我的问题是:

1)什么能引起0代堆的大小报道能长得这么大?

1) What can cause the reported size of the Gen 0 Heap to grow so large?

2)这是一个问题吗?特别是,它可能会导致大量未使用的空间被分配到.NET?

2) Is this a problem? In particular, could it be causing a large amount of unused space to be allocated to .NET?

3)如果是的话,我应该怎么做才能解决这个问题?如果我不能prevent从使用的Application_Start期间的内存,我想至少能够让它放弃内存时,应用程序启动完成的过程。

3) If so, what should I do to fix it? If I can't prevent the process from using that much memory during Application_Start, I'd like to at least be able to make it give up the memory when app start completes.

推荐答案

第0级包含了最年轻的,最近分配的对象,并且是独立和独特的LOH。这听起来好像是你分配吨,吨小物件(所有这些高速缓存条目相关的勘误表,通过它的声音),这是无根(由频繁GC的饲养规模下降证明),但没有清理及时,因为GC并没有认为有必要呢。在GC只是目前还没有看到一个需要清理。多少RAM有时机器有哪些?我猜,你是不是分页。

Gen 0 contains the "the youngest, most recently allocated objects", and is separate and distinct from the LOH. What it sounds like is that you are allocating tons and tons of small objects (all of the errata associated with those cache entries, by the sound of it) which are unrooted (demonstrated by the frequent GC's keeping the size down) but not cleaned up in a timely manner because the GC hasn't deemed it necessary yet. The GC simply has not yet seen a need to clean up. How much RAM does the machine have? I am guessing that you are not paging.

我的理解是,.NET可以将未使用的堆块到操作系统时,GC认为它不再需要(因为这堆空间尚未被使用很长一段时间,例如)。你有没有过的很长一段时间观察到的应用程序,看看是否出现这种情况?如果你没有分页,我不认为这是一个问题。

My understanding is that .NET can return chunks of unused heap to the OS when the GC deems it is no longer needed (because that heap space hasn't been used for a long time, for example). Have you observed the app over a long period of time, to see whether this happens? If you are not paging, I don't think it is a problem.

这篇关于大根0堆在.NET中,浪费内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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