java 內存溢出總結

jvm

/**
 * 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);
    }
}

 

 

方法區(永久代)spa

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);
    }
}

 

 

code

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);
    }
}

相關文章
相關標籤/搜索