HeapOutOfMemory
代码:
package spark.jvm;
import java.util.ArrayList;
import java.util.List;
class Person{ }
public class HeapOutOfMemory {
public static void main(String[] args) {
System.out.println("HeapOutMemory");
List<Person> persons= new ArrayList<Person>();
int count = 0;
while(true){
persons.add(new Person());
System.out.println("Instance: " + (++count));
}
}
}
ideaVM相关配置:
-verbose:gc -Xms10M -Xmx10M -XX:MaxDirectMemorySize=5M -Xss128k -XX:+PrintGCDetails
错误:
StackOverFlow
代码:
package spark.jvm;
public class StackOverFlow {
private int counter;
public void count(){
counter++;
count();
}
public static void main(String[] args) {
System.out.println("StackOverFlow");
StackOverFlow stackOverFlow = new StackOverFlow();
try {
stackOverFlow.count();
}catch (Exception e){
e.printStackTrace();
throw e;
}
}
}
错误:
ConstantOutOfMemory
代码:
package spark.jvm;
import java.util.ArrayList;
import java.util.List;
public class ConstantOutOfMemory {
public static void main(String[] args) {
try {
List<String> stringList = new ArrayList<String>();
int item = 0;
while(true){
stringList.add(String.valueOf(item++).intern());
}
}catch (Exception e){
e.printStackTrace();
throw e;
}
}
}
问题:
DirectMemoryOutOfmemory
代码:
package spark.jvm;
import java.nio.ByteBuffer;
public class DirectMemoryOutOfmemory {
private static final int ONE_MB = 1024*1024*1024;
private static int count = 1;
public static void main(String[] args) {
try {
while (true){
ByteBuffer buffer = ByteBuffer.allocateDirect(ONE_MB);
count++;
}
}catch (Exception e){
System.out.println("Exception:instance created " +count);
e.printStackTrace();
}catch (Error e){
System.out.println("Error:instance created "+count);
e.printStackTrace();
}
}
}
问题: