views:

148

answers:

4

MY application profiling showing a big Garbage Collection overhead. The profiler does'nt have a drill down into the Garbage Collection. What should I do to reduce this overhead?

I have many short-living arraylists and some long-lived that only die when the application shuts down.

+5  A: 

If you have too much garbage collection overhead, reduce your garbage. Try reusing lists ( preallocate and use them, clear them when done).

If you are using ArrayList with value types, try switching to use List<T> instead.

BioBuckyBall
There must be something else wrong with their algorithm, even when I write trash code for experiments I never really have to worry about the GC.
ChaosPandion
certainly the most likely case :)
BioBuckyBall
@ChaosPandion: Unless, of course, they're working on a (semi-)non-standard platform. For example, the GC on the XBox360 in XNA tends to have problems very frequently...
Reed Copsey
@Reed - Touche.
ChaosPandion
@Reed: If this is for XNA the OP should certainly have said so. W/o such an indication you can only assume the 'normal' framework.
Henk Holterman
What % of overall GC performance overhead is considered more??The profiler shows a certain % for GC but does'nt indicate what part of code is causing extra load for it. I have'nt used profilers a lot, may be some hints can help me drill down on the real cause
bsobaid
+2  A: 

If garbage collection overhead becomes a serious performance issue, then you have to look at your design and re-asses the amount of short-lived objects you are creating.

chibacity
A: 

If this application runs as a service, or else performs a large amount of work before returning to the user interface, you may need to change your garbage collection model.

Without additional details, it's hard to give a good recommendation.

ThatBlairGuy
+1  A: 

Well basically you should reduce the work for the garbage collector. There a certain 'patterns' which produce a lot of work.

  • Avoid having many objects with finalizers. Finalizers impose additional work on the garbage collector, because a object with a finalizer has to be collected twice.
  • Avoid the 'midlife'-crisis. The .NET GC (on the desktop) is generational GC. When the object survive the first collection, but 'die' shortly after, the GC has done a lot of work for nothing. (coping to the second generation, collecting again, etc). So try to optimize the life-time of you objects in a way that they either die very quickly, or survive for a long, long time.
  • Reduce unnecessary allocations. For example by using value type wisely. Or doing the work in a less memory-intensive way.

So in your case I guess that you either you've a 'midlife'-crisis with the short lived lists. Or you simple allocate list like mad.

In the first case: Try to make the life-span of the lists shorter. I can't tell you how the solution looks like for your application.

In the second case: Try to avoid allocation so many lists. Maybe you can use proper value-types? Or fixed sized arrays? Or change the structure of the code in such a way that less lists are needed?

Anyway, I would recommend to profile you applicaition and look how much you memory you allocate and how much can be collected in the first generation.

Gamlor
A short-lived span is the one where a list is created new() inside a function ? and consequently goes out of scope and become eligible for collection once the function is out of scope?or length of the function also matters? if function is big, an arraylist may not get collected in the first generation collection even if the function was instantiated inside the function?
bsobaid
What matters is if there's still a reference to the object. The length of the function has nearly no influence. Of course, when you've real long method, which first allocates the object, then does a lot of other work and meanwhile the object never goes out of scope, its possible that the object is moved to the second generation. But with your everyday code this should be a problem.
Gamlor
"What matters is if there's still a reference to the object."By object you mean the object kept by the arraylist? yes, that object remains and outlives the function.
bsobaid