栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

11-直接内存

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

11-直接内存

直接内存概述

①. 不是虚拟机运行时数据区的一部分,也不是《Java虚拟机规范》中定义的内存区域

②. 直接内存是Java堆外的、直接向系统申请的内存区间

③. 代码演示:

public class BufferTest {
    private static final int BUFFER = 1024 * 1024 * 1024;//1GB

    public static void main(String[] args){
        //直接分配本地内存空间
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER);
        System.out.println("直接内存分配完毕,请求指示!");

        Scanner scanner = new Scanner(System.in);
        scanner.next();

        System.out.println("直接内存开始释放!");
        byteBuffer = null;
        System.gc();
        scanner.next();
    }
}

④. 来源于NIO,通过存在堆中的DirectByteBuffer操作Native内存

public class BufferTest1 {
    private static final String TO = "d:\2022.1.24-结业典礼录像.7z";
    private static final int _100Mb = 1024 * 1024 * 1024;

    public static void main(String[] args) {
        long sum = 0;
        String src = TO;
        for (int i = 0; i < 3; i++) {
            String desc = "d:\2022.1.24-结业典礼录像" + i + ".7z";
//            sum += io(src, desc);
            sum += directBuffer(src, desc);
        }
        System.out.println("总共花费的时间:" + sum);
    }

    
    private static long directBuffer(String src, String desc) {
        long start = System.currentTimeMillis();
        FileChannel inChannel=null;
        FileChannel outChannel=null;

        try {
            inChannel=new FileInputStream(src).getChannel();
            outChannel=new FileOutputStream(desc).getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_100Mb);
            while (inChannel.read(byteBuffer)!=-1){
                byteBuffer.flip();//修改数据模式
                outChannel.write(byteBuffer);
                byteBuffer.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (inChannel!=null){
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outChannel!=null){
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        long end = System.currentTimeMillis();

        return end - start;
    }

    
    private static long io(String src, String desc) {
        long start = System.currentTimeMillis();
        try {
            FileInputStream fis = new FileInputStream(src);
            FileOutputStream fos = new FileOutputStream(desc);
            int len;
            byte[] buffer=new byte[_100Mb];
            while ((len=fis.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

        }

        long end = System.currentTimeMillis();

        return end - start;
    }
}

⑤. 通常,访问直接内存的速度会优于Java堆。即读写性能高

因此出于性能考虑,读写频繁的场合可能会考虑使用直接内存。Java 的 NIO 库允许 Java 程序使用直接内存,用于数据缓冲区 也可能导致 OutOfMemoryError 异常

OutOfMemoryError: Direct buffer memory
public class BufferTest2 {
private static final int BUFFER = 1024 * 1024 * 2;//20mb

public static void main(String[] args) {
    List list = new ArrayList<>();
    int count = 0;
    try {
        while (true) {
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER);
            list.add(byteBuffer);
            count++;
            Thread.sleep(100);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.out.println(count);
    }

}

}
```
由于直接内存在 Java 堆外,因此它的大小不会直接受限于 -Xmx 指定的最大堆大小,但是系统内存是有限的, Java 堆和直接内存的总和依然受限于操作系统能给出的最大内存。

缺点

分配回收成本较高不受 JVM 内存回收管理 ⑥. 直接内存大小可以通过MaxDirectMemorySize设置,如果不指定,默认与堆的最大值一Xmx参数值一致

public class MaxDirectMemorySize {
    private static final long _1MB = 1024 * 1024;

    public static void main(String[] args) throws IllegalAccessException {
        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeField.get(null);
        while (true) {
            unsafe.allocateMemory(_1MB);
        }
    }
}

⑦. 简单理解: java process memory = java heap + native memory

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756176.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号