-
package com.shi.jvm;
-
public class StackTest {
-
/**
-
* 递归调用
-
*/
-
public static void add() {
-
add();
-
}
-
/**
-
* 栈满了异常
-
* Exception in thread "main" java.lang.StackOverflowError
-
* @param args
-
*/
-
public static void main(String[] args) {
-
add();
-
}
-
}
-
package com.shi.jvm;
-
public class HeapTest {
-
public static void main(String[] args) {
-
/**
-
* 堆内存异常
-
* 输出结果:当前最大可用内存多少M : 1724
-
* Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
-
* at com.shi.jvm.HeapTest.main(HeapTest.java:8)
-
*/
-
System.out.println("当前最大可用内存多少M : "+Runtime.getRuntime().maxMemory()/1024/1024);
-
//创建一个大对象
-
byte[] b = new byte[1*1024*1024*1725];
-
System.out.println("大对象创建成功");
-
}
-
}
-
package com.shi.jvm;
-
import java.util.Random;
-
/**
-
* -Xms1024m -Xmx1024m -XX:+PrintGCDetails
-
堆初始化内存,堆最大内存 打印出堆GC回收信息
-
* @author shiye
-
*
-
*/
-
public class HeapSpaceTest {
-
public static void main(String[] args) {
-
/**
-
* 输出: MAX_MEMERY = 1808269312(字节)1724.5MB
-
TOTAL_MEMERY = 122683392(字节)117.0MB
-
* @param args
-
*/
-
// long maxMemory = Runtime.getRuntime().maxMemory();//返回Java虚拟机试图使用的最大内存量
-
// long totalMemory = Runtime.getRuntime().totalMemory();//返回当前使用的java虚拟机的内存总量
-
// System.out.println("MAX_MEMERY = " + maxMemory +"(字节)" + (maxMemory/(double)1024/1024) + "MB");
-
// System.out.println("TOTAL_MEMERY = " + totalMemory +"(字节)" + (totalMemory/(double)1024/1024) + "MB");
-
//-Xms8m -Xmx8m -XX:+PrintGCDetails
-
String str = "www.shiye";
-
while(true) {
-
str += str + new Random().nextInt(9999);
-
}
-
/**
-
* 结果: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
-
*/
-
}
-
}