欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > JVM(HotSpot):直接内存及其使用建议

JVM(HotSpot):直接内存及其使用建议

2024/10/23 16:30:03 来源:https://blog.csdn.net/Brave_heart4pzj/article/details/142985122  浏览:    关键词:JVM(HotSpot):直接内存及其使用建议

文章目录

  • 一、什么是直接内存?
  • 二、特点
  • 三、使用案例
  • 四、直接内存的管理

一、什么是直接内存?

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 线程通过 Cleanerclean 方法调
    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);}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com