当解决托管堆碎片碎片

2023-09-03 01:45:30 作者:中国好骚年

我正在读一本博客条目由约什 - 史密斯,他使用了缓存机制,以降低托管堆碎片的。他的缓存减少在稍慢的执行速度的成本正在创建短命的对象的数量。

如何的问题多少是托管堆碎片像C#中的托管语言?你如何诊断,如果这是一个问题?在什么情况下你会通常需要处理呢?

解决方案   

不要太快。这通常是非常便宜的有短暂的对象。对于一个高速缓存是有利可图的存在将不得不(很)多candiadates,他们应该活足够长的时间,使其下一代。

  您

如何诊断,如果这是一个问题?

通过探查。我不太确定这篇文章的作者这样做。

  

如何的问题多少是托管堆碎片像C#中的托管语言?

据我所知,这是罕见的。 NET有一个压缩垃圾收集器,即prevents大多数形式的碎片。没有与大对象问题,堆的时候。

编辑:

王者资源溢出问题天美不解决,玩家自创方案,1个月出售8千碎片

当你通过下面的文章的评论,你会发现有人测量了一下,发现缓存比每次创建新的EventArgs慢了许多。

结论:测量,然后再开始优化。这不是一个好主意/例子。

I was reading a blog entry by Josh Smith where he used a cache mechanism in order to "reduce managed heap fragmentation". His caching reduces the number of short-lived objects being created at the cost of slightly slower execution speed.

How much of a problem is managed heap fragmentation in a managed language like C#? How can you diagnose if it's an issue? In what situations would you typically need to address it?

解决方案

When

Not too quickly. It is generally very cheap to have short lived objects. For a cache to be profitable there would have to be (very) many candiadates and they should live long enough to make it to the next generation.

How can you diagnose if it's an issue?

With a Profiler. I'm not so sure the author of the article did that.

How much of a problem is managed heap fragmentation in a managed language like C#?

As far as I know it is rare. .NET has a compacting Garbage collector, that prevents most forms of fragmentation. There are issues with the Large Object Heap sometimes.

Edit:

When you go through the comments below the article you will find that someone measured it and found the cache to be a lot slower than creating new eventargs each time.

Conclusion: Measure before you start optimizing. This was not a good idea/example.