Java Garbage Collector

Java Garbage Collector


Garbage Collectors in Java

Cleanup in Java

  • GC can process only in the heap area
  • GC is running its own thread when it feels memory is running low

Types of GCs

  • Serial GC
  • Parallel GC
  • CMS (Concurrent Mark Swap) collector
  • G1 GC (Garbage First)
  • Z GC

Serial GC

  • Uses a single thread
  • Uses mark-copy for the young generation and mark-swap-compact for the old generation
  • When executing it freezes all other threads until GC finishes.
  • Efficient because no communication overhead between threads
  • Suitable for small applications
  • Use java -XX:+UseSerialGC to use this GC

Parallel GC

  • Same as Serial GC, but this has multiple threads to speed up the process.
  • Uses mark-copy for the young generation and mark-swap-compact for the old generation
  • This was the default GC until Java 8.
  • Suitable for multi-core machines and if you need better performance.
  • Use java -XX:+UseParallelGC to use this GC

CMS (Concurrent Mark Swap) collector

  • This is an improved and advanced version of parallel GC
  • Many threads are being used to scan heap memory
  • This is running using Mark and Swap algorithm
    • Marking live objects
    • Removing unreachable objects
  • How it works
    • Define GC roots. Ex. Active threads, local variables/ input variables of executing methods... etc.
    • Traverse through the object graph in the memory
    • The application thread must stop to traverse
    • This is called Stop The World pause
    • GC visited object marked as live
    • Now free up the memory, releasing unused memory. 
    • There are different ways to do this
      • Normal deletion
      • Deletion with compacting
      • Deletion with coping
  • This is deprecated in JDK 9
  • Use java -XX:+UseConcMarkSweepGC to use this GC

G1 GC (Garbage First)

  • Introduced in Java 7 to replace the CMS collector
  • Mostly used in multi-processor machines with a large amount of data
  • This divides the heap into several equal-sized heaps and collects the regions with lesser live data.
  • Most garbage-contained sections will be cleaned first.
  • Since Java 9, this is the default GC.
  • Use java XX:+UseG1GC to use this GC

Z GC

  • This is available since JDK 11
  • This performs all expensive works concurrently without stopping the execution.
  • Use java -XX:+UseZGC to use this GC

How to select a GC?

  • It is better to allow the virtual machine to select the GC
  • If necessary, adjust the heap size to improve performance.
  • If that is also not enough, select a GC as follows ,according to the official document
    • If the application has small data set (100MB) select Serial GC
    • If the application runs on a single processor and no pause time requirement, then select Serial GC
    • If the peak application performance is the first priority and there is no pause time then select Parallel GC
    • If the response time is more important, then select G1 GC
    • If the response time is the highest priority, then select Z1 GC

Possible causes for java.lang.OutOfMemoryError: Java heap space

  • Configuration issues (heap size may be not enough)
  • The application is holding objects without any usages
  • Excessive use of finalize() methods.



0 comments :

Post a Comment