您的位置:首页 > 编程语言 > Java开发

Java深入---Java堆溢出以及垃圾回收机制

2015-05-28 10:15 603 查看
</pre><pre name="code" class="java">import java.util.ArrayList;
import java.util.List;
/**
* @author MohnSnow
* @time 2015年5月27日 下午5:39:17
*
*/
/*
* 测试Java堆溢出
*
* JVM-SET: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails
* -XX:SurvivorRatio=8
* OOM: OutOfMemoryError
*/
public class Test {
static class OOMObject {
static {
System.out.println("test;");
}
}
public static void main(String[] args) {
List<OOMObject> list = new ArrayList<OOMObject>();
while (true) {
list.add(new OOMObject());
}
}
}

/**
* @author MohnSnow
* @time 2015年5月28日 上午10:39:17
*
*/
/*
* 垃圾回收机制中的引用记数算法:给对象添加一个引用计数器,每当一个地方引用他时,计数器就加一;当引用失效时候,就减一;任何时刻计数器为零的对象,就是不可能在被使用的
* 。
*/
public class Test {
public Object instance = null;
private static final int _1MB = 1024 * 1024;
public byte[] bigSize = new byte[2 * _1MB];
public static void testGC() {
Test objA = new Test();
Test objB = new Test();
objA.instance = objB;
objB.instance = objA;
objA = null;
objB = null;
System.gc();
}

public static void main(String[] args) {
testGC();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: