Thursday, 5 December 2024

JVM Optimization


Understand Your Application's Behavior


Profile Your Application

Use profiling tools to understand memory usage patterns and identify memory leaks. VisualVM can be used for the same


        

https://github.com/manaspratimdas/memory-analysis/tree/master/myappsmem/src/main/java/myappsmem/heapexhaustion/ml

 

Monitor GC Logs

Regularly analyze GC logs to understand the frequency and duration of GC. While running the application we can enable GC logging with below JVM argument as follows 

-Xlog:gc*:file=gclog/gc_%t_%p.log


Choose the Right Garbage Collector

  •    Serial GC: -XX:+UseSerialGC: Suitable for small applications with low memory requirements.
  •    Parallel GC: -XX:+UseParallelGC : Good for applications with high throughput requirements.
  •    CMS GC: -XX:+UseConcMarkSweepGC : Suitable for applications requiring low pause times.
  •    G1 GC: -XX:+UseG1GC : A balanced option for applications with large heaps and requiring predictable pause

      Illustration: Serial GC vs G1 GC

https://github.com/manaspratimdas/memory-analysis/blob/master/myappsmem/src/main/java/myappsmem/optimization/MyAppMemAnalyzer.java

Run the program with JVM arguments 

  • -Xlog:gc*:file=gclog/gc_%t_%p.log -XX:+UseSerialGC
  • -Xlog:gc*:file=gclog/gc_%t_%p.log -XX:+UseG1GC



Tune Heap Size


Set Initial and Maximum Heap Size

Use `-Xms` and `-Xmx` to set the initial and maximum heap size. It's often recommended to set them to the same value to avoid resizing during runtime

  • -Xlog:gc*:file=gclog/gc_%t_%p.log (Default) [ it took xms as 254 and xmx as 4048]
  • -Xms16m -Xmx512m -Xlog:gc*:file=gclog/gc_%t_%p.log
  • -Xms64m -Xmx64m -Xlog:gc*:file=gclog/gc_%t_%p.log







Adjust Young Generation Size

Use `-XX:NewSize` and `-XX:MaxNewSize` to tune the size of the young generation. A larger young generation can reduce the frequency of minor GC

  • -XX:NewSize=128m -XX:MaxNewSize=128m: No GCs occurred, indicating that the memory allocation was sufficient to avoid GC events.
  • -XX:NewSize=64m -XX:MaxNewSize=64m: One GC event occurred, suggesting that the memory allocation was almost sufficient but required one cleanup.
  • -XX:NewSize=16m -XX:MaxNewSize=16m: Seven GC events occurred, indicating that the memory allocation was insufficient, leading to frequent GCs.

When the number of garbage collections (GC) increases, it can significantly impact the performance of your application
  • GC Pause Time: Frequent GCs cause more pauses, degrading application responsiveness and throughput.
  • CPU Usage: Higher GC frequency increases CPU usage, as more time is spent on memory management.
  • Latency: More frequent GCs lead to higher latency, affecting real-time performance.
  • Memory Fragmentation: Frequent GCs can cause memory fragmentation, slowing down memory








No comments:

Post a Comment

Streaming with Kafka API

The Kafka Streams API is a Java library for building real-time applications and microservices that efficiently process and analyze large-sca...