java local direct memory overflow

DirectMemory capacity by -XX: MaxDirectMemorySize specify, if not specified, the default is the same as the maximum java heap, although using DirectByteBuffer allocate memory memory overflow will throw an exception, but it does not really apply to the allocation of the operating system when an exception is thrown memory, but the memory can not be allocated by that calculation, then manually throw an exception, the real application allocates memory approach is unsafe.allocateMemory ()
Here is the test code:

public class DirectMemoryOOM{
	private static final int_1MB = 1024 * 1024;
	public static void main(String[] args) throws Exception{
		Field unsafeField = Unsafe.class.getDeclaredFields()[0];
		unsafeField.setAccessible(true);
		Unsafe unsafe = (Unsafe) unsafeField.get(null);
		while(true){
			unsafe.allocateMemory(_1MB);
		}
	}
}

Execution error:

Excepiton in thead "main" java.lang.OutOfMemoryError

Guess you like

Origin blog.csdn.net/fight252/article/details/91351098