原因看高"在GC&QUOT%的时间;在逆足星期一 [英] Reasons for seeing high "% Time in GC" in Perf Mon

查看:135
本文介绍了原因看高"在GC&QUOT%的时间;在逆足星期一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而在逆足周一监控我们的应用我注意到时间的百分比在GC 20随时随地为 - 60%,而我们的应用程序正在执行长时间运行的过程(30秒之间变化到1.5分钟)。这似乎有点过分了我。这就提出了两个重要的问题。




  1. 我是纠正这种过度?

  2. 路线我可以带弄清楚为什么这么多的GC是怎么回事?


解决方案

是的,这听起来确实过高。减少GC的数量很可能是你可以采取减少应用程序的运行时间(如果这是你的目标)的最好的一步。



一个较高的%的时间在气相色谱法通常是通过分配,然后扔掉数千或数百万个对象而引起的。找出发生了什么事情的一个好方法是使用一个内存分析器工具。



Microsoft提供了免费的CLR探查的。这将显示每个分配,反而会使你的应用程序运行10-60倍慢。您可能需要在更少的输入数据运行,以便它能够完成在合理时间内分析。



一个伟大的商业工具,是赛特的的 .NET内存分析器的。这规定要少得多运行时开销,并且有可用的免费试用。通过采取当你的进程正在运行多个快照,你可以找到正被频繁分配对象的类型(再破坏)。



一旦你确定的来源该分配的,那么你需要检查代码,并找出如何分配这些可以减少。虽然没有一个放之四海而皆准的所有答案,有些事情我已经在过去遇到的包括:




  • String.Split能创建数百个小型短暂的字符串。如果你正在做大量的字符串操作,它可以帮助字符逐个字符走它来处理字符串。

  • 创建的数以千计的小类别的阵列或列表(例如,在大小24个字节),可以是昂贵的;如果这些类可以作为值类型进行治疗,它可以(有时)大大提高的东西来改变他们结构。

  • 创建成千上万的小阵列,可以增加内存使用了很多(因为每个阵列有开销的少量);有时这些可以用一个大阵和索引到它的一个子部分进行更换。

  • 拥有大量终结对象(特别是如果他们没有被设置),可以放很多对垃圾收集器的压力;确保你正确地处理所有IDisposable的对象,并注意自己的类型应该(几乎)从未有终结的。

  • 微软拥有的垃圾收集指南提高性能。


While monitoring our application in Perf Mon I noticed that the % of Time In GC is anywhere from 20 - 60% while our application is performing a long running process (varies between 30 seconds to 1.5 minutes). This seems a bit excessive to me. This raises two important questions.

  1. Am I correct that this excessive?
  2. Routes I can take to figure out why so much GC is happening?

解决方案

Yes, this does sound excessive. Reducing the amount of GC would probably be the single best step you could take to reducing the runtime of your application (if that is your goal).

A high "% time in GC" is typically caused by allocating and then throwing away thousands or millions of objects. A good way to find out what's going on is to use a memory profiler tool.

Microsoft provides the free CLR Profiler. This will show you every allocation, but will make your app run 10-60 times slower. You may need to run it on less input data so that it can finish analyzing in a reasonable amount of time.

A great commercial tool is SciTech's .NET Memory Profiler. This imposes much less runtime overhead, and there is a free trial available. By taking multiple snapshots while your process is running, you can find out what type of objects are being frequently allocated (and then destroyed).

Once you've identified the source of the allocations, you then need to examine the code and figure out how those allocations can be reduced. While there are no one-size-fits-all answers, some things I've encountered in the past include:

  • String.Split can create hundreds of small short-lived strings. If you're doing a lot of string manipulation, it can help to process the string by walking it character-by-character.
  • Creating arrays or lists of thousands of small classes (say, under 24 bytes in size) can be expensive; if those classes can be treated as value types, it can (sometimes) greatly improve things to change them to structs.
  • Creating thousands of small arrays can increase memory usage a lot (because each array has a small amount of overhead); sometimes these can be replaced with one large array and indexes into a sub-section of it.
  • Having a lot of finalizable objects (particularly if they're not being disposed) can put a lot of pressure on the garbage collector; ensure that you're correctly disposing all IDisposable objects, and note that your own types should (almost) never have finalizers.
  • Microsoft has an article with Garbage Collection Guidelines for improving performance.

这篇关于原因看高"在GC&QUOT%的时间;在逆足星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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