Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce Garbage Collection performance Overhead

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.

like image 571
bsobaid Avatar asked Jul 16 '10 17:07

bsobaid


People also ask

How does garbage collection affect performance?

An application that spends 1% of its execution time on garbage collection will loose more than 20% throughput on a 32-processor system. If we increase the GC time to 2%, the overall throughput will drop by another 20%. Such is the impact of suspending 32 executing threads simultaneously!

What is garbage collection overhead?

Simply put, the JVM takes care of freeing up memory when objects are no longer being used. This process is called Garbage Collection (GC). The GC Overhead Limit Exceeded error is one from the java. lang. OutOfMemoryError family, and it's an indication of a resource (memory) exhaustion.


2 Answers

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.

like image 190
Gamlor Avatar answered Nov 07 '22 14:11

Gamlor


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.

like image 39
µBio Avatar answered Nov 07 '22 12:11

µBio