public class Block { private static Unsafe unsafe; { try { unsafe = getUnsafe(); } catch (Throwable e) { throw new Error("fatal: " + e.getMessage(), e); } } private int size; private long p; private int offset; public static Block newInstance(int size) { return new Block(size); } /** * 2 * 1024 * 1024 * * @param size * @throws Throwable */ public Block(int size) { p = unsafe.allocateMemory(size); this.size = size; offset = 0; } public static Unsafe getUnsafe() throws Throwable { Class<?> unsafeClass = Unsafe.class; for (Field f : unsafeClass.getDeclaredFields()) { if ("theUnsafe".equals(f.getName())) { f.setAccessible(true); return (Unsafe) f.get(null); } } throw new IllegalAccessException("no declared field: theUnsafe"); } public synchronized byte[] get() { int nbytes = offset; byte[] bytes = new byte[nbytes]; for (int i = 0; i < nbytes; i++) { bytes[i] = unsafe.getByte(p + i); } return bytes; } public synchronized void put(byte[] bytes) { int retainBytes = size - offset; int nbytes = retainBytes > bytes.length ? bytes.length : retainBytes; for (int i = 0; i < nbytes; i++) { unsafe.putByte(p + offset++, bytes[i]); } } public synchronized boolean available() { return p != -1; } public synchronized void deallocate() { unsafe.freeMemory(p); p = -1; offset = -1; } }
@Test public void test() throws Throwable { Block block = Block.newInstance(2 * 1024 * 1024); System.out.println(block.available()); block.put("greeting".getBytes()); byte[] bytes = block.get(); String s = new String(bytes); System.out.println(s); block.deallocate(); System.out.println(block.available()); block.put("helloworld".getBytes()); }
测试代码输出:
true
greeting
false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d37c4a6, pid=5584, tid=0x000012a0
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) Client VM (25.101-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0x13c4a6]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\home\admin\workstation\java\javatest\hs_err_pid5584.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
greeting
false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d37c4a6, pid=5584, tid=0x000012a0
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) Client VM (25.101-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0x13c4a6]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\home\admin\workstation\java\javatest\hs_err_pid5584.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
@Test public void test1() throws Throwable { Block block = Block.newInstance(5); System.out.println(block.available()); block.put("greeting".getBytes()); byte[] bytes = block.get(); String s = new String(bytes); System.out.println(s); block.deallocate(); System.out.println(block.available()); block.put("helloworld".getBytes()); }
测试代码输出
true
greet
false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d37c4a6, pid=5412, tid=0x00001074
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) Client VM (25.101-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0x13c4a6]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\home\admin\workstation\java\javatest\hs_err_pid5412.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
greet
false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d37c4a6, pid=5412, tid=0x00001074
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) Client VM (25.101-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0x13c4a6]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\home\admin\workstation\java\javatest\hs_err_pid5412.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#