标签:操作系统 本地变量 system 关系 栈空间 分析 while 通过 单线程
/** * -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError */ public class HeapOOM { static class OOMObject { Object obj = new Object(); } public static void main(String[] args) { List<OOMObject> list = new ArrayList<OOMObject>(); while( true) { list.add( new OOMObject()); } } }
/** * -Xss128k * 使用-Xss减小栈内存容量. * 定义大量本地变量,增加方法帧中本地变量表长度. */ public class JavaVMStackSOF { private int stackLength = 1; public static void main(String[] args) { JavaVMStackSOF javaVMStackSOF = new JavaVMStackSOF(); try { javaVMStackSOF.test(); } catch(java.lang.StackOverflowError e) { System. out.println(javaVMStackSOF. stackLength); } } public void test() { ++ stackLength; test(); } }
/** * -XX:PermSize=10M -XX:MaxPermSize=10M * @author Administrator * */ public class RuntimeConstantPoolOOM { public static void main(String[] args) { List<String> list = new ArrayList<String>(); int i = 0; while( true) { list.add(String. valueOf(i++).intern()); } } }
/** * -XX:PermSize=10M -XX:MaxPermSize=10M */ public class JavaMethodAreaOOM { public static void main(String[] args) { while( true) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(OOMObject. class); enhancer.setUseCache( false); enhancer.create(); } } static class OOMObject { } }
/** * -Xmx20M -XX:MaxDirectMemorySize=10M */ public class DirectMemoryOOM { private static final int _1MB = 1024*1024; public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException { Field unsafeField = sun.misc.Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible( true); sun.misc.Unsafe unsafe = (sun.misc.Unsafe)unsafeField.get( null); while( true) { unsafe.allocateMemory( _1MB); } } }
当方法参数很多时候,不便于阅读和维护,因此建议使用一个JavaBean来包装入参。这个建议的另外一方面因素请先考虑下面这个问题
public class MyBean { private final double a; private final double b; public MyBean(double a, double b) { this.a = a; this.b = b; } public double getA() { return a; } public double getB() { return b; } }
public class StackOverflowTestA { private static int invokeMethodCount = 0; public static void method(MyBean myBean) { ++invokeMethodCount; System.out.println(invokeMethodCount); //method(new MyBean(myBean.getA(), myBean.getB())); method(myBean); } public static void main(String[] args) { try { method(new MyBean(1L, 2L)); } catch(Throwable e) { System.out.println("调用次数 : " + invokeMethodCount); } } }
public class StackOverflowTestB { private static int invokeMethodCount = 0; public static void method(long a, long b) { ++invokeMethodCount; System.out.println(invokeMethodCount); method(a, b); } public static void main(String[] args) { try { method(1L, 2L); } catch(Throwable e) { System.out.println("调用次数 : " + invokeMethodCount); } } }
我们都知道Java虚拟机栈的空间有限,当出现无限递归的代码时就会发生StackOverflowError错误。
那么请思考上面代码中StackOverflowTestA和StackOverflowTestB哪一个递归的深度更深一些?如果StackOverflowTestB中method方法入参变为两个short类型呢?
了解OutOfMemoryError异常 - 深入Java虚拟机读后总结
标签:操作系统 本地变量 system 关系 栈空间 分析 while 通过 单线程
原文地址:http://www.cnblogs.com/sealedbook/p/6285999.html