Master JVM Startup Flags: Decode GC Logs and Optimize Memory

This guide explains the most common JVM startup flags for controlling garbage collection logging, heap sizing, class loading monitoring, and out‑of‑memory handling, providing sample outputs and detailed interpretations to help developers tune memory usage and diagnose performance issues in large Java applications.

Programmer DD
Programmer DD
Programmer DD
Master JVM Startup Flags: Decode GC Logs and Optimize Memory

Preface

When the JVM starts, it uses a set of default parameters that are sufficient for most projects, but large applications often require manual adjustments such as increasing heap size or enabling GC logs to troubleshoot mysterious crashes.

Parameter Overview

1. -verbose:gc

Enables printing of GC information during JVM startup. Example output:

[Full GC 178K->99K(1984K), 0.0253877 secs]
Interpretation: "Full GC" indicates a full collection; 178K and 99K are memory before and after GC; 1984K is total heap capacity; the final number is the time taken in seconds.

2. -XX:+PrintGC

Prints GC information similar to the previous flag; no additional description needed.

3. -XX:+PrintGCDetails

Provides detailed GC information. Example output:

–Heap<br/>– def new generation   total 13824K, used 11223K [0x27e80000, 0x28d80000, 0x28d80000)<br/>–  eden space 12288K,  91% used [0x27e80000, 0x28975f20, 0x28a80000)<br/>–  from space 1536K,   0% used [0x28a80000, 0x28a80000, 0x28c00000)<br/>–  to   space 1536K,   0% used [0x28c00000, 0x28c00000, 0x28d80000)<br/>– tenured generation   total 5120K, used 0K [0x28d80000, 0x29280000, 0x34680000)<br/>–   the space 5120K,   0% used [0x28d80000, 0x28d80000, 0x28d80200, 0x29280000)<br/>– compacting perm gen  total 12288K, used 142K [0x34680000, 0x35280000, 0x38680000)<br/>–   the space 12288K,   1% used [0x34680000, 0x346a3a90, 0x346a3c00, 0x35280000)<br/>–    ro space 10240K,  44% used [0x38680000, 0x38af73f0, 0x38af7400, 0x39080000)<br/>–    rw space 12288K,  52% used [0x39080000, 0x396cdd28, 0x396cde00, 0x39c80000)
Interpretation: "new generation" is the young generation of the heap; "total" shows its total size, "used" shows current usage, and the three hexadecimal values represent the start, current, and maximum boundaries. "eden space" (often translated as Eden) holds newly created objects; its size and usage are shown. "from space" and "to space" are the two survivor spaces used by the copying collector. "tenured generation" refers to the old generation. "compacting perm" denotes the permanent generation.

4. -XX:+PrintGCTimeStamps

Prints timestamps for each GC event. Example:

289.556: [GC [PSYoungGen: 314113K->15937K(300928K)] 405513K->107901K(407680K), 0.0178568 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
293.271: [GC [PSYoungGen: 300865K->6577K(310720K)] 392829K->108873K(417472K), 0.0176464 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]
Interpretation: The leading number is the elapsed time since JVM start. "GC" indicates a minor (young‑generation) collection; "PSYoungGen" shows the Parallel Scavenge collector. The memory figures follow the same pattern as earlier, showing before/after sizes for the young generation and the whole heap. The "Times" section reports user, system, and real time consumed by the GC.

5. -Xloggc:log/gc.log

Specifies the file path where GC logs are written (e.g., log/gc.log in the current directory).

6. -XX:+PrintHeapAtGC

Prints heap information after each GC event, using a format similar to the previous flags.

7. -XX:+TraceClassLoading

Monitors class loading. Example output:

•[Loaded java.lang.Object from shared objects file]
•[Loaded java.io.Serializable from shared objects file]
•[Loaded java.lang.Comparable from shared objects file]
•[Loaded java.lang.CharSequence from shared objects file]
•[Loaded java.lang.String from shared objects file]
•[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
•[Loaded java.lang.reflect.Type from shared objects file]

This flag makes it clear which classes have been loaded.

8. -XX:+PrintClassHistogram

When triggered (e.g., by Ctrl+Break), prints a histogram of class instances:

num     #instances         #bytes  class name
----------------------------------------------
  1:        890617      470266000  [B
  2:        890643       21375432  java.util.HashMap$Node
  3:        890608       14249728  java.lang.Long
  4:            13        8389712  [Ljava.util.HashMap$Node;
  5:     2062371680  [C
  6:       46341904  java.lang.Class

The columns show the index, instance count, total size in bytes, and class name. Types B and C correspond to byte and char arrays.

9. -Xmx and -Xms

Set the maximum and minimum heap size. The JVM starts with the minimum size and expands toward the maximum as needed after several GC cycles.

10. -Xmn

Specifies the size of the young generation.

11. -XX:NewRatio

Defines the ratio between young and old generations (e.g., 1:4 means the young generation occupies one‑fifth of the heap).

12. -XX:SurvivorRatio

Sets the ratio between each survivor space and the Eden space (e.g., 2:8 means each survivor occupies one‑tenth of Eden).

13. -XX:+HeapDumpOnOutOfMemoryError

Generates a heap dump file when an OutOfMemoryError occurs.

14. -XX:+HeapDumpPath

Specifies the file path for the heap dump.

15. -XX:OnOutOfMemoryError

Executes a custom script when an OOM event happens, useful for actions such as generating thread dumps, sending alerts, or restarting services.

16. -XX:PermSize and -XX:MaxPermSize

Set the initial and maximum size of the permanent generation; exhausting this space can also cause OOM.

17. -Xss

Sets the thread stack size, typically a few hundred kilobytes per thread.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJVMperformanceMemory ManagementgcStartup Flags
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.