开发者

.NET garbage profiling: calculate how much garbage a piece of code is making

I would like to write a utility class that will help me determine how much garbage a certain piece of code is making.

Something I can use like this: Garba开发者_StackOverflow社区geProfiler.Start(); and int numGarbage = GarbageProfiler.End();

Idea is simple enough. But does it make sense to do so? Are there any tools out there to do just this?


But does it make sense to do so?

No.

You would at least have to differentiate between the Generations of the garbage.

The memory that a certain piece of code allocates and still holds at the end is (can be) much more expensive than the objects it quickly released.


There are quite a few memory profilers already out there. A quick search on Google will give you several.

There is even an API for memory profiling .Net applications.


The amount of garbage that is behind a reference will change from run to run depending on whether the reference holds that last reference(s) to the objects or not. So predicting the amount of garbage is very hard.

There are also quite some performance counters that you can read just before a GC.Collect() and after it that could give you an idea.

I do question the use of this all; you will never know when the GC kicks in so you do not know when the garbage will be recycled. And calling GC.Collect() is not a very good idea (most of the time)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜