堆外内存使用实例
所属分类 java
浏览量 1481
堆外内存2种使用方式
使用Unsafe ,需要自己释放
使用 NIO ByteBuffe ,依赖full gc 回收
Unsafe unsafe = null;
try{
unsafe = Unsafe.getUnsafe();
}catch(Throwable e){
System.out.println("getUnsafe error,"+e);
}
// 反射获取
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
unsafe = (Unsafe) f.get(null);
long address = unsafe.allocateMemory(1024);
unsafe.putInt(address, 7);
unsafe.putInt(address+4, 8);
System.out.println(unsafe.getInt(address));
unsafe.freeMemory(address);
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
buffer.putChar('a');
buffer.putInt(3);
buffer.getInt();
完整代码
https://gitee.com/dyyx/hellocode/blob/master/src/dyyx/test/UnsafeTest.java
https://gitee.com/dyyx/hellocode/blob/master/src/dyyx/test/DirectBufferTest.java
参考资料
unsafe要点
DirectByteBuffer申请与释放
java堆外内存回收机制
netty线程模型和零拷贝机制
NIO JMX BufferPool内存监控
上一篇
下一篇
sql优化建议
jvm codecache 相关整理
JVM MemoryUsage中init,committed,used,max说明
redis优化要点
devops简介及工具链
jenkins简介