Search code examples
javaperformancegarbage-collection

Garbage Collector taking too much CPU Time


I've developed a Web Application which process a huge amount of data and takes a lot of time to complete?

So now I am doing profiling of my application and I noticed one very bad thing about GC.
When a Full GC occurred it stops all process for 30 - 40 secs.

I wonder if there is any way to improve this. I don't want to waste my CPU's that much time only in GC. Below are some details that can be useful:

  1. I am using Java 1.6.0.23
  2. My Application takes 20 GB max memory.
  3. A full GC occur after every 14 minutes.
  4. Memory Before GC is 20 GB and after GC is 7.8 GB
  5. Memory used in CPU (i.e. shown in task manager) is 41 GB.
  6. After process completed(JVM is still running) Used memory 5 GB and free memory 15 GB.

Solution

  • The fewer things you new, the fewer things need to be collected.

    Suppose you have class A. You can include in it a reference to another instance of class A. That way you can make a "free list" of instances of A. Whenever you need an A, just pop one off the free list. If the free list is empty, then new one.

    When you no longer need it, push it on the free list.

    This can save a lot of time.