假设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:CMSInitiatingOcc XX: PrintGCApplicationStoppedTime -XX: PrintHeapAtGC -XX: PrintGCDateStamps -XX: PrintHeapAtGC -XX: PrintGCDateStamps -verbose:gc -XX: PrintGCDetails -Xloggc:/Users/##test*#/temp##. #
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但是並不能回收。
我最近看到兩篇文章,應該對你有用:
http://zhuanlan.51cto.com/art...
http://zhuanlan.51cto.com/art...