Garbage Collection is Extremely Slow Due to System Swap Activity
Applies to
Java Platform, Standard Edition - Version 1.4.2 and later
Any platform
Introduction
Garbage Collection (GC) takes a very long time to complete. During this
time any application threads are all paused and the application seems to
hang. If the Java option -XX:+PrintGCDetails is used to report the GC
activity, a very long “real” time will be reported, while “user” and
“sys” times are much smaller:
[PSYoungGen: 33333K->0K(2649536K)] [PSOldGen: 3947221K->2898114K(5120000K)] 3980555K->2898114K(7769536K) [PSPermGen: 524287K->222489K(524288K)], 1245.9340459 secs] [Times: user=10.46 sys=0.99, real=1245.94 secs]
Cause
When a system is short of free physical memory it will swap memory pages to a swap partition or swap file on disk. During the garbage collection, the Java object tree is traversed to determine which objects can be freed. If parts of the heap are swapped to disk, then those pages must be reloaded back into RAM to be examined. This will potentially cause other pages of the Java heap to be swapped out. Since objects, which can reference each other, are spread throughout the heap, object traversal will lead to significant swapping activity if the entire Java heap cannot be accommodated in physical memory.
Solutions
The goal should be to avoid any memory swapping activity. There are two ways to achieve this:
- Increase the memory available to the JVM by:
- Adding more physical memory
- If the Java Virtual Machine (JVM) is running in a virtual system (VM), allocating more memory to the VM.
- Reducing the system load by eliminating other memory-consuming applications
- Alternatively, you can reduce native memory footprint of the JVM by:
- Decreasing Java heap size (-Xmx/-Xms), lowering the number of Java threads, and/or reducing Java thread stack sizes (-Xss)
Note: Configuring bigger swap areas, swap files, pagefile etc. will NOT help as none of these actions increase the amount of physical memory available to contain the Java heap.
Last reviewed on Sat Feb 01 2025 00:00:00 GMT+0000 (Coordinated Universal Time)