java 内存溢出总结

/**
 * jvm 参数: -Xms5m -Xmx5m -Xmn2m -XX:NewSize=1m
 * @author admin
 *
 */
public class HeapOutOfMemoryError {
    
    //
    @SuppressWarnings("unused")
    private String[] strings = new String[100000];
    
    public static void main(String[] args) {
        Map<Object, HeapOutOfMemoryError> map = new HashMap<Object, HeapOutOfMemoryError>();
        int i = 0;
        
        do {
            map.put(String.valueOf(i), new HeapOutOfMemoryError());
            i++;
        } while (i<100000);
    }
}

 

 

方法区(永久代)

public class PermOutOfMemoryError {

    // 方法区
    @SuppressWarnings("unused")
    private static String[] strings = new String[100000];
    
    public static void main(String[] args) {
        Map<Object, PermOutOfMemoryError> map = new HashMap<Object, PermOutOfMemoryError>();
        int i = 0;
        
        do {
            map.put(String.valueOf(i), new PermOutOfMemoryError());
            i++;
        } while (i<100000);
    }
}

 

 

public class StackOverFlowError {

    public static void main(String[] args) {
        int i = 0;
        go(i);
    }

    @SuppressWarnings("unused")
    private static void go(int i) {
        System.out.println(i);
        String[] strings = new String[100000];
        i++;
        go(i);
    }
}

 

posted @ 2019-05-01 16:47  xiluhua  阅读(206)  评论(0编辑  收藏  举报