G1 垃圾收集器:为什么 survivor space 总是满的?

G1 Garbage Collector: Why survivor space is always full?

这是 jmap -heap 命令的输出:

Survivor Space:
   regions  = 52
   capacity = 54525952 (52.0MB)
   used     = 54525952 (52.0MB)
   free     = 0 (0.0MB)
   100.0% used

我执行了很多次,发现capacity的值总是等于used

我的问题是为什么幸存者 space 总是满的(而且这么小)?我指定了 -Xmx2200m -Xms2200m -Xmn1100m。 (我预计 survivor space 应该是 220M,这意味着 survivor 区域应该有更多 space)

--更新--

jheap 的完整输出:

Garbage-First (G1) GC with 2 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 2306867200 (2200.0MB)
   NewSize                  = 1153433600 (1100.0MB)
   MaxNewSize               = 1153433600 (1100.0MB)
   OldSize                  = 4194304 (4.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 16777216 (16.0MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 4294963200 (4095.99609375MB)
   G1HeapRegionSize         = 1048576 (1.0MB)

Heap Usage:
G1 Heap:
   regions  = 14488
   capacity = 15191769088 (14488.0MB)
   used     = 1083703304 (1033.5000076293945MB)
   free     = 14108065784 (13454.499992370605MB)
   7.13348983730946% used
G1 Young Generation:
Eden Space:
   regions  = 988
   capacity = 1163919360 (1110.0MB)
   used     = 1035993088 (988.0MB)
   free     = 127926272 (122.0MB)
   89.009009009009% used
Survivor Space:
   regions  = 45
   capacity = 47185920 (45.0MB)
   used     = 47185920 (45.0MB)
   free     = 0 (0.0MB)
   100.0% used
G1 Old Generation:
   regions  = 2
   capacity = 1095761920 (1045.0MB)
   used     = 524296 (0.5000076293945312MB)
   free     = 1095237624 (1044.4999923706055MB)
   0.04784762003775419% used

30424 interned Strings occupying 3027304 bytes.

我最好的猜测是您的应用程序正在创建大量对象并且伊甸园 Space 很快就会填满(或者您的应用程序正在创建大量在 Young GC 中存活的长期对象)。

在 Young GC 期间,活动对象将始终移动到其中一个幸存者 space。对象将被移动到 Old Gen,只有当对象足够老或者不是来自 Eden Space 的所有对象都不能放入选定的幸存者 spaces 时。

您介意分享一下您要解决的真正问题是什么,或者您只是想知道为什么幸存者 space 已满。

我的观察。

  1. 当您停止自定义某些参数时,G1GC 是高效的。我建议删除 Xmn(年轻一代)设置。

  2. 设置-Xms-Xmx

    不同的值
  3. 区域大小应配置为最大堆内存大小/2048。对于 4G 堆,区域大小应为 2 MB;对于 2 GB 堆,区域大小应为 1 MB。

  4. 要配置的关键参数:-XX:G1HeapRegionSize=n, XX:MaxGCPauseMillis=m, -XX:ParallelGCThreads=n, -XX:ConcGCThreads=n 除了-Xms and -Xmx

查看此 SE post and infoQ article 了解更多详情。

来自Oracle

的推荐

在评估和微调 G1 GC 时,请牢记以下建议:

Young Generation Size: Avoid explicitly setting young generation size with the -Xmn option or any or other related option such as -XX:NewRatio. Fixing the size of the young generation overrides the target pause-time goal.

Pause Time Goals: When you evaluate or tune any garbage collection, there is always a latency versus throughput trade-off.