假设我有非常简单的代码:
public class Sandbox {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
while (true) {
map.put(new Random().toString(), new Random().nextInt());
}
}
}
它有多“不正确”并不重要,重要的是它做了什么。它产生一个
OutOfMemory
, 最终。我运行它:java -Xms50M -Xmx50M "-Xlog:heap*=debug" "-Xlog:gc*=debug" Sandbox
会有很多日志,我会剪到我关心的最重要的部分(为了方便阅读,我也去掉了一些行中的一些信息)
***************************** GC(0) *********************************
[0.115s][debug][gc,heap] GC(0) Heap before GC invocations=0 (full 0)
[0.115s][debug][gc,heap] GC(0) region size 1024K, 23 young, 0 survivors
[0.127s][info ][gc,heap] GC(0) Eden regions: 23->0(15)
[0.128s][info ][gc,heap] GC(0) Survivor regions: 0->3(3)
[0.128s][info ][gc,heap] GC(0) Old regions: 0->4
[0.128s][info ][gc,heap] GC(0) Archive regions: 2->2
[0.128s][info ][gc,heap] GC(0) Humongous regions: 1->1
[0.128s][debug][gc,heap] GC(0) Heap after GC invocations=1 (full 0):
[0.128s][debug][gc,heap] GC(0) region size 1024K, 3 young, 3 survivors
***************************** GC(1) *********************************
[0.159s][debug][gc,heap] GC(1) Heap before GC invocations=1 (full 0)
[0.159s][debug][gc,heap] GC(1) region size 1024K, 18 young, 3 survivors
[0.181s][info ][gc,heap] GC(1) Eden regions: 15->0(1)
[0.181s][info ][gc,heap] GC(1) Survivor regions: 3->3(3)
[0.181s][info ][gc,heap] GC(1) Old regions: 4->10
[0.181s][info ][gc,heap] GC(1) Archive regions: 2->2
[0.181s][info ][gc,heap] GC(1) Humongous regions: 3->3
[0.181s][debug][gc,heap] GC(1) Heap after GC invocations=2 (full 0)
[0.181s][debug][gc,heap] GC(1) region size 1024K, 3 young, 3 survivors
***************************** GC(2) *********************************
[0.182s][debug][gc,heap] GC(2) Heap before GC invocations=2 (full 0)
[0.182s][debug][gc,heap] GC(2) region size 1024K, 4 young, 3 survivors
[0.190s][info ][gc,heap] GC(2) Eden regions: 1->0(9)
[0.190s][info ][gc,heap] GC(2) Survivor regions: 3->1(1)
[0.190s][info ][gc,heap] GC(2) Old regions: 10->13
[0.190s][info ][gc,heap] GC(2) Archive regions: 2->2
[0.190s][info ][gc,heap] GC(2) Humongous regions: 3->3
[0.190s][debug][gc,heap] GC(2) Heap after GC invocations=3 (full 0)
[0.190s][debug][gc,heap] GC(2) region size 1024K, 1 young, 1 survivors
它们究竟是什么意思?
最佳答案
我只在网上找到了两个引用资料,以某种方式解释了这一点:一个 here和 other here .不幸的是,它们都没有多大意义,所以我必须查看生成它的源代码才能理解。
第一行:
[0.115s][debug][gc,heap] GC(0) Heap before GC invocations=0 (full 0)
[0.115s][debug][gc,heap] GC(0) region size 1024K, 23 young, 0 survivors
告诉你,在第一次调用GC(
0
)之前,年轻空间有23
分配的区域。它还告诉您(间接地)在这 23 个中: 0
是幸存者区域,意思是 23
分别是 Eden
那些。接下来的几行:
[0.127s][info ][gc,heap] GC(0) Eden regions: 23 -> 0 (15)
[0.128s][info ][gc,heap] GC(0) Survivor regions: 0 -> 3 (3)
告诉你之前
GC operation
,有23
伊甸园地区。所有这些都被清除了(即 0
)(毕竟,这就是年轻 GC 存在的原因)。然后它表明在GC之前有0
幸存者区域,但由于此循环,3 Survivor Regions
被生成。你必须格外小心这意味着什么。这不会显示有多少区域可用于下一个周期,而只会显示发生的转换。
这些日志中解释了这种“转换”:
[0.128s][debug][gc,heap] GC(0) Heap after GC invocations=1 (full 0):
[0.128s][debug][gc,heap] GC(0) region size 1024K, 3 young, 3 survivors
注意堆是如何转换到
3 young, 3 survivors
的(因此, 0 eden
,所有 23
都被清除了)。之后还有一步:调整区域总数。如何?你可以看看source code yourself ,一探究竟。
特别是
(3)
将被计算 here更多细节(和非常好的评论)here ,它基本上是说多少 Survivor Regions
将可用于下一个 GC 周期。在大多数情况下,(3)
将等于 ->3
,对于幸存者区域。这个“调整”是关于
(15)
和 (3)
,这意味着下一个周期会有 15 Eden Regions
和 3 Survivor Regions
;也由下一个周期的日志表示,在它的最开始。[0.159s][debug][gc,heap] GC(1) Heap before GC invocations=1 (full 0)
[0.159s][debug][gc,heap] GC(1) region size 1024K, 18 young, 3 survivors
我需要在这里做一个评论:这些是对下一个循环中 GC 假设使用什么的提示,它们可能会被忽略(例如,
humongous allocation
会导致这种情况)。所以我们现在可以画一条线了。这些日志中有两个逻辑部分。
现在应该很容易消化一些日志,例如:
[0.182s][debug][gc,heap] GC(2) Heap before GC invocations=2 (full 0)
[0.182s][debug][gc,heap] GC(2) region size 1024K, 4 young, 3 survivors
它始于
4 young = 1 Eden + 3 Survivor
[0.190s][info ][gc,heap] GC(2) Eden regions: 1 -> 0 (9)
[0.190s][info ][gc,heap] GC(2) Survivor regions: 3 -> 1 (1)
它过渡到
0 Eden, 1 Survivor
[0.190s][info ][gc,heap] GC(2) Eden regions: 1 -> 0 (9)
[0.190s][info ][gc,heap] GC(2) Survivor regions: 3 -> 1 (1)
它应用启发式生成
9 Eden, 1 Survivor
进入下一个 GC 循环。
关于java - 如何使用 "-Xlog:heap*=debug" "-Xlog:gc*=debug"从 GC 正确读取日志的某些部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60567878/