简体   繁体   中英

Java big object garbage collection

I have JVM argument (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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]

Looking at GC log, there are always 20M occupied in heap. however there should be 10M live object after each iteration.

Could you help me understand why there is 20M occupied?

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. Only occasionally do we see one array still retained after GC (that's the entries which show around 20000 K after the arrow).

The numbers you have chosen to emphasize in boldface are the detailed results of specific GC algorithms running. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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