Java native memory

 

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
#

 

@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
#

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值