java直接内存使用

概述: 

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

  2.直接内存是java堆外的,直接向系统申请的内存区间。

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

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

    出于性能考虑,读写频繁的场合可能会考虑使用直接内存。

    java的NIO库允许java程序使用直接内存

  5.直接内存也会导致oom异常

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

    直接内存的缺点:

    分配/回收成本较高

  7.不受jvm内存回收管理

  8.直接内存大小可以通过MaxDirectMemorySize设置

  9.如果不指定,默认与堆的最大值-Xmx参数值一致

public class BufferTest {

    private static final String TO = "F:\test\1.5gtest.mp4";
    private static final int _100Mb = 1024 * 1024 * 100;

    public static void main(String[] args) {
        long sum = 0;
        String src = "F:\test\1.5gtest.mp4";
        for (int i = 0; i < 3; i++) {
            String dest = "F:\test\1.5gtest_" + i + ".mp4";
//            sum += io(src,dest);//54606
            sum += directBuffer(src,dest);//50244
        }

        System.out.println("总花费的时间为:" + sum );
    }

    private static long directBuffer(String src,String dest) {
        long start = System.currentTimeMillis();

        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            inChannel = new FileInputStream(src).getChannel();
            outChannel = new FileOutputStream(dest).getChannel();

            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_100Mb);
            while (inChannel.read(byteBuffer) != -1) {
                byteBuffer.flip();//修改为读数据模式
                outChannel.write(byteBuffer);
                byteBuffer.clear();//清空
            }
        } catch (IOException 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 dest) {
        long start = System.currentTimeMillis();

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);
            byte[] buffer = new byte[_100Mb];
            while (true) {
                int len = fis.read(buffer);
                if (len == -1) {
                    break;
                }
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }


        long end = System.currentTimeMillis();

        return end - start;
    }
}
原文地址:https://www.cnblogs.com/zyf-yxm/p/13660978.html