wordpress添加文章总数标签总数,云平台网站优化,南宁网络公司联系方式,上海外贸公司企业名录文章目录 一、什么是直接内存#xff1f;二、特点三、使用案例四、直接内存的管理 一、什么是直接内存#xff1f;
Direct Memory#xff1a;系统内存
普通IO#xff0c;运行原理图 磁盘到系统内存#xff0c;系统内存到jvm内存。 NIO#xff0c;运行原理图 划分了一块… 文章目录 一、什么是直接内存二、特点三、使用案例四、直接内存的管理 一、什么是直接内存
Direct Memory系统内存
普通IO运行原理图 磁盘到系统内存系统内存到jvm内存。 NIO运行原理图 划分了一块区域JVM和系统共享的内存区间这样就减少了一次IO操作。
二、特点
常见于 NIO 操作时用于数据缓冲区 分配回收成本较高但读写性能高 不受 JVM 内存回收管理
所以我们可以在IO程序中使用直接内存来优化程序的读写性能。
三、使用案例
关键代码ByteBuffer.allocateDirect(_1Mb);
public class Demo1_9 {static final String FROM E:\\sbPSjI4tt10.mp4;static final String TO E:\\a.mp4;static final int _1Mb 1024 * 1024;public static void main(String[] args) {io(); // io 用时1535.586957 1766.963399 1359.240226directBuffer(); // directBuffer 用时479.295165 702.291454 562.56592}private static void directBuffer() {long start System.nanoTime();try (FileChannel from new FileInputStream(FROM).getChannel();FileChannel to new FileOutputStream(TO).getChannel();) {ByteBuffer bb ByteBuffer.allocateDirect(_1Mb);while (true) {int len from.read(bb);if (len -1) {break;}bb.flip();to.write(bb);bb.clear();}} catch (IOException e) {e.printStackTrace();}long end System.nanoTime();System.out.println(directBuffer 用时 (end - start) / 1000_000.0);}private static void io() {long start System.nanoTime();try (FileInputStream from new FileInputStream(FROM);FileOutputStream to new FileOutputStream(TO);) {byte[] buf new byte[_1Mb];while (true) {int len from.read(buf);if (len -1) {break;}to.write(buf, 0, len);}} catch (IOException e) {e.printStackTrace();}long end System.nanoTime();System.out.println(io 用时 (end - start) / 1000_000.0);}
}但是直接内存是不受JVM管理的 另外我们显示调用gcJVM也不是立马就执行gc。
而且一般我们会在项目中禁用显示调用gc因为Full GC影响性能。 禁用参数-XX:DisableExplicitGC
四、直接内存的管理
底层是如何回收直接内存的
使用了 Unsafe 对象完成直接内存的分配回收并且回收需要主动调用 freeMemory 方法ByteBuffer 的实现类内部使用了 Cleaner 虚引用来监测 ByteBuffer 对象一旦 ByteBuffer 对象被垃圾回收那么就会由 ReferenceHandler 线程通过 Cleaner 的 clean 方法调 用 freeMemory 来释放直接内存 我们知道不建议程序员显示调用gc来回收JVM对象。 但是等待JVM自主的Full GC又是不确定的。 所以还是建议我们自己手动回收直接内存。
public class Demo1_27 {static int _1Gb 1024 * 1024 * 1024;public static void main(String[] args) throws IOException {Unsafe unsafe getUnsafe();// 分配内存long base unsafe.allocateMemory(_1Gb);unsafe.setMemory(base, _1Gb, (byte) 0);System.in.read();// 释放内存unsafe.freeMemory(base);System.in.read();}public static Unsafe getUnsafe() {try {Field f Unsafe.class.getDeclaredField(theUnsafe);f.setAccessible(true);Unsafe unsafe (Unsafe) f.get(null);return unsafe;} catch (NoSuchFieldException | IllegalAccessException e) {throw new RuntimeException(e);}}
}