JVM异常之:直接内存溢出

示例:

package com.dxz.jvm;

import java.lang.reflect.Field;
import sun.misc.Unsafe;

/**
 * @Described:直接内存溢出测试
 * @VM args: -Xmx20M -XX:MaxDirectMemorySize=10M
 */
public class DirectoryMemoryOutOfmemory {

    private static final int ONE_MB = 1024 * 1024;
    private static int count = 1;

    public static void main(String[] args) {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            Unsafe unsafe = (Unsafe) field.get(null);
            while (true) {
                unsafe.allocateMemory(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();
        }
    }

}

 

posted on 2018-03-07 15:27  duanxz  阅读(1085)  评论(0编辑  收藏  举报