java - ArrayList内存分配,跨代引用gc问题
问题描述
ArrayList的在JVM中内存分配是怎么样的?假设List<BigObject> list = new ArrayList<>(); private static class BigObject{private byte[] foo;BigObject(){ foo = new byte[20 * 1024];} }
不断add BigObject,其引用关系是如下吗?
然后发生ygc,是BigObject进入了old区不,如果arrayList对象没进入old区,那岂不是就出现跨代引用了? 如果进入了,那ygc后又obj = new BigObject()在新生代分配, 然后list.add(obj), 岂不是会出现old->young区的引用,其内存分配究竟是怎么样的?
问题缘起运行下面这段代码:参数:/jdk1.7.0_79.jdk/bin/java -Xmx500M -Xms500M -Xmn200M -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=90 -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime -XX:+PrintHeapAtGC -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -XX:+PrintGCDateStamps -verbose:gc -XX:+PrintGCDetails -Xloggc:/Users/*/temp/test.gc.log
import java.util.ArrayList;import java.util.List;/** * @author *** * @time 2017-04-19-11:14 */public class CrossReference { private static int unit = 20 * 1024; public static void main(String[] args) {allocate();try { Thread.sleep(1000); System.out.println('allocate end************'); Thread.sleep(100000);} catch (Exception e){ e.printStackTrace();} } private static void allocate(){List<BigObject> list = new ArrayList<>();int size = 1024 * 1024 * 400;int len = size / unit;for( int i = 0; i < len; i++){ BigObject bigObject = new BigObject(); list.add(bigObject);} } private static class BigObject{private byte[] foo;BigObject(){ foo = new byte[unit]; //} }}
查看gc.log将会出现在执行完allocate()方法后,old区仍然占用在90%以上,不断进行CMS GC但是并不能回收。
问题解答
回答1:我最近看到两篇文章,应该对你有用:
http://zhuanlan.51cto.com/art...http://zhuanlan.51cto.com/art...