jvisualvm添加visualgc插件,查看内容的状态转移
默认新生代:老生代比例为1:2,默认eden区和s1,s2区的比例为 8:1:1,通过设置很小的jvm堆参数,比如9M,这样新生代为3M,老生代为6M
设置一个对象为1M,并且不释放内容,可以很快看到内存的走向,从eden区转向s1,再移到old区,经过几次之后old区也满了,就产生fullgc,接着oom
public class Visual { byte[] bytes = new byte[1024 * 1024 * 1]; public static void main(String[] args) throws InterruptedException { case2(); } private static void case1() throws InterruptedException { List<Visual> list = new ArrayList<>(); for (; ; ) { list.add(new Visual()); System.out.println(new Date().toString()); Thread.sleep(100L); } } private static void case2() throws InterruptedException { List<Visual> list = new ArrayList<>(); for (; ; ) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 1000; i++) { list.add(new Visual()); try { Thread.sleep(100L); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); Thread.sleep(10000L); } } }