简体   繁体   English

Java大对象垃圾回收

[英]Java big object garbage collection

I have JVM argument (JVM 1.8.20) 我有JVM参数(JVM 1.8.20)

-Xmx40M -Xms40M -verbose:gc   -XX:+PrintGCDetails

and code: 和代码:

public static void main(String[] args) {
        byte[] memory = null;
        int MB = 1024 * 1024;
        int c = 0;
        while(c++<10){          
            memory = new byte[(int) (10 * MB)];         
        }   

    }

There are GC after each iteration: 每次迭代后都有GC:

[GC (Allocation Failure) [DefNew: 932K->472K(12288K), 0.0010986 secs] 932K->472K(39616K), 0.0011674 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC(分配失败)[DefNew:932K-> 472K(12288K),0.0010986秒] 932K-> 472K(39616K),0.0011674秒] [时间:用户= 0.00 sys = 0.00,实际= 0.00秒]

[GC (Allocation Failure) [DefNew: 10931K->472K(12288K), 0.0053905 secs] 10931K->10712K(39616K), 0.0054285 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] [GC(分配失败)[DefNew:10931K-> 472K(12288K),0.0053905秒] 10931K-> 10712K(39616K),0.0054285秒] [时间:用户= 0.00 sys = 0.00,实际= 0.01秒]

[GC (Allocation Failure) [DefNew: 10712K->472K(12288K), 0.0057686 secs] 20952K->20952K(39616K), 0.0058082 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] [GC(分配失败)[DefNew:10712K-> 472K(12288K),0.0057686秒] 20952K-> 20952K(39616K),0.0058082秒] [时间:用户= 0.01 sys = 0.00,实际= 0.00秒]

[GC (Allocation Failure) [DefNew: 10712K->10712K(12288K), 0.0000173 secs][Tenured: 20480K->10711K(27328K), 0.0041701 secs] 31192K->10711K(39616K), [Metaspace: 85K->85K(4480K)], 0.0042462 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC(分配失败)[DefNew:10712K-> 10712K(12288K),0.0000173秒] [使用期限:20480K-> 10711K(27328K),0.0041701秒] 31192K-> 10711K(39616K),[元数据:85K-> 85K( 4480K)],0.0042462秒] [时间:用户= 0.00 sys = 0.00,实际= 0.00秒]

[GC (Allocation Failure) [DefNew: 10240K->0K(12288K), 0.0033846 secs] 20951K->20951K(39616K), 0.0034223 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC(分配失败)[DefNew:10240K-> 0K(12288K),0.0033846秒] 20951K-> 20951K(39616K),0.0034223秒] [时间:用户= 0.00 sys = 0.00,实际= 0.00秒]

[GC (Allocation Failure) [DefNew: 10240K-> 10240K (12288K), 0.0000158 secs][Tenured: 20951K-> 10711K (27328K), 0.0040890 secs] 31191K->10711K(39616K), [Metaspace: 85K->85K(4480K)], 0.0041566 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] [GC(分配失败)[DefNew:10240K-> 10240K (12288K),0.0000158秒] [使用期:20951K-> 10711K (27328K),0.0040890秒] 31191K-> 10711K(39616K),[元数据:85K-> 85K( 4480K)],0.0041566秒] [时间:用户= 0.02 sys = 0.00,实际= 0.00秒]

[GC (Allocation Failure) [DefNew: 10240K->0K(12288K), 0.0026607 secs] 20951K->20951K(39616K), 0.0026945 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC(分配失败)[DefNew:10240K-> 0K(12288K),0.0026607秒] 20951K-> 20951K(39616K),0.0026945秒] [时间:用户= 0.00 sys = 0.00,实际= 0.00秒]

[GC (Allocation Failure) [DefNew: 10240K-> 10240K (12288K), 0.0000169 secs][Tenured: 20951K-> 10711K (27328K), 0.0039672 secs] 31191K->10711K(39616K), [Metaspace: 85K->85K(4480K)], 0.0040413 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [GC(分配失败)[DefNew:10240K-> 10240K (12288K),0.0000169秒] [使用期:20951K-> 10711K (27328K),0.0039672秒] 31191K-> 10711K(39616K),[元数据:85K-> 85K( 4480K)],0.0040413秒] [时间:user = 0.00 sys = 0.00,real = 0.00秒]

[GC (Allocation Failure) [DefNew: 10240K->0K(12288K), 0.0026438 secs] 20951K-> 20951K (39616K), 0.0026780 secs] [Times: user=0.01 sys=0.00, real=0.00 secs] [GC(分配失败)[DefNew:10240K-> 0K(12288K),0.0026438秒] 20951K-> 20951K (39616K),0.0026780秒] [时间:用户= 0.01 sys = 0.00,实际= 0.00秒]

[GC (Allocation Failure) [DefNew: 10240K-> 10240K (12288K), 0.0000158 secs][Tenured: 20951K-> 10711K (27328K), 0.0037923 secs] 31191K->10711K(39616K), [Metaspace: 85K->85K(4480K)], 0.0038584 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] [GC(分配失败)[DefNew:10240K-> 10240K (12288K),0.0000158秒] [使用期:20951K-> 10711K (27328K),0.0037923秒] 31191K-> 10711K(39616K),[元数据:85K-> 85K( 4480K)],0.0038584秒] [时间:用户= 0.00 sys = 0.00,实际= 0.01秒]

Looking at GC log, there are always 20M occupied in heap. 查看GC日志,堆中总是有20M占用。 however there should be 10M live object after each iteration. 但是,每次迭代后应该有1000万个活动对象。

Could you help me understand why there is 20M occupied? 您能帮我理解为什么有20M占用了吗?

I think you are misinterpreting the log. 我认为您在误解日志。 Most of the time you've got 10M occupancy, and that's your baseline—what the runtime uses at idle. 在大多数情况下,您的使用量为10M,这就是您的基线-运行时在空闲状态下使用的内容。 Only occasionally do we see one array still retained after GC (that's the entries which show around 20000 K after the arrow). 只有偶尔我们会看到在GC之后仍然保留一个数组(这就是箭头后约20000 K处显示的条目)。

The numbers you have chosen to emphasize in boldface are the detailed results of specific GC algorithms running. 您选择以粗体强调的数字是运行特定GC算法的详细结果。 The only relevant numbers are those which are singly nested within square brackets. 唯一相关的数字是那些单独嵌套在方括号中的数字。

I would also point out that your code is such that the array is never retained, it becomes garbage the moment it is created. 我还要指出的是,您的代码是这样的:数组永远不会保留,创建后就变成垃圾。 This is because memory is a local variable and you never read from it. 这是因为memory是局部变量,并且您从不会读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM