[1]System.gc()

 

System.gc()会触发FullGC,回收新生代、老年代。(还有方法区)

以如下代码为例

 Java Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//-Xms16M -Xmx16M -Xmn8m -XX:SurvivorRatio=2 -XX:+PrintHeapAtGC -XX:-UseTLAB -XX:+UseSerialGC
//
新生代8M//eden4M //from/to2M
//
老年代8M
package com.shadowfaxghh.test;

import java.text.DecimalFormat;

public class Main {

    
public static void main(String[] args) {
        
        
byte[] a=new byte[1*1024*1024];//分配1M到新生代上
        a=null;
        
        
byte[] b=new byte[6*1024*1024];//分配6M到老年代上
        b=null;
        
        System.gc();
        
    }
}

 

可以看到触发了FullGC

 [1]System.gc()