java直接内存

Java中的内存从广义上可以划分为两个部分,一部分是受JVM管理堆内存,另一部分则是不受JVM管理的堆外内存,也称为直接内存。直接内存由操作系统来管理,这部分内存的应用可以减少垃圾收集对应用程序的影响。

直接内存概述

直接内存不是虚拟机运行时数据区的一部分,也不是Java虚拟机规范中定义的内存区域。直接内存是在Java堆外的、直接向操作系统申请的内存区间。直接内存来源于NIO(Non-Blocking IO),可以通过ByteBuffer类操作。ByteBuffer类调用allocateDirect()方法可以申请直接内存,方法内部创建了一个DirectByteBuffer对象,DirectByteBuffer对象存储直接内存的起始地址和大小,据此就可以操作直接内存。直接内存和堆内存之间的关系如图所示。

在这里插入图片描述

查看直接内存的占用和释放

import java.nio.ByteBuffer;
import java.util.Scanner;

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();
    }
}

直接分配后的本地内存空间如图所示:

在这里插入图片描述

释放内存后的本地内存空间如图所示:

在这里插入图片描述

通常,访问直接内存的速度会优于Java堆,读写性能更高。因此出于性能考虑,读写频繁的场合可能会考虑使用直接内存。Java的NIO库允许Java程序使用直接内存,用于数据缓冲区。

通过前面的案例我们可以把Java进程占用的内存理解为两部分,分别是JVM内存和直接内存。前面我们讲解方法区的时候,不同版本JDK中方法区的实现是不一样的,JDK 7使用永久代实现方法区,永久代中的数据还是使用JVM内存存储数据JDK 8使用元空间实现方法区,元空间中的数据放在了本地内存当中,直接内存和元空间一样都属于堆外内存

在这里插入图片描述

直接内存的优势

文件读写必然涉及磁盘的读写,但是Java本身不具备磁盘读写的能力,因此借助操作系统提供的方法,在Java中表现出来的形式就是Java中的本地方法接口调用本地方法库。普通IO读取一份物理磁盘的文件到内存中,需要下面两步。

(1)把磁盘文件中的数据读取到系统内存中。

(2)把系统内存中的数据读取到JVM堆内存中。

如图所示,为了使得数据可以在系统内存和JVM堆内存之间相互复制,需要在系统内存和JVM堆内存都复制一份磁盘文件。这样做不仅浪费空间,而且传输效率低下。

在这里插入图片描述

当使用NIO时,如图所示。操作系统划出一块直接缓冲区可以被Java代码直接访问。这样当读取文件的时候步骤如下。

(1)物理磁盘读取文件到直接内存。

(2)JVM通过NIO库直接访问数据。

以上步骤便省略了系统内存和JVM内存直接互相复制的过程,不仅节省了内存空间,也提高了数据传输效率。可以这样理解:直接内存是在系统内存和Java堆内存之间开辟出的一块共享区域,供操作系统和Java代码访问。

在这里插入图片描述

IO和NIO的性能比较

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class BufferTest1 {
    private static final int _100Mb = 1024 * 1024 * 100;

    public static void main(String[] args) {
        String userHome = System.getProperty("user.home");
        long sum = 0;
        String src = userHome + "/Downloads/JDK/jdk8.tar.gz";
        for (int i = 0; i < 3; i++) {
            String dest = userHome + "/Downloads/JDK/jdk8.tar.gz" + i;
            //sum += io(src,dest);//1515
            sum += directBuffer(src, dest);//1022
            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;
    }
}

直接内存异常

直接内存也可能导致OutOfMemoryError异常。由于直接内存在Java堆外,因此它的大小不会直接受限于-Xmx指定的最大堆大小,但是系统内存也是有限的,Java堆和直接内存的总和依然受限于操作系统能给出的最大内存。接下来通过一个案例演示直接内存的内存溢出现象,如代码清单所示。

import sun.misc.VM;

import java.nio.ByteBuffer;
import java.util.ArrayList;

/**
 * 直接内存内存的OOM: OutOfMemoryError:Direct buffer memory
 *
 * -XX:MaxDirectMemorySize 设置最大直接内存
 * -Xmx60m 设置最大堆内存
 * 默认 MaxDirectMemorySize 等于 Xmx
 */
public class BufferTest2 {
    private static final int BUFFER = 1024 * 1024 * 20;//20MB

    public static void main(String[] args) {
        // 获取Java虚拟机中的Runtime实例
        Runtime runtime = Runtime.getRuntime();

        // 获取JVM的最大内存
        long maxMemory = runtime.maxMemory();
        System.out.println("Max Memory: " + maxMemory + " bytes");

        long maxDirectMemory = VM.maxDirectMemory();
        System.out.println("Max Direct Memory: " + maxDirectMemory + " bytes");
        ArrayList<ByteBuffer> list = new ArrayList<>();
        int count = 0;
        try {
            while (true) {
                ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER);
                list.add(byteBuffer);
                count++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            System.out.println(count);
        }
    }
}

直接内存由于不受JVM的内存管理,所以需要开发人员自己来管理,以防内存溢出,通常有两种方式。

(1)当ByteBuffer对象不再使用的时候置为null,调用System.gc()方法告诉JVM可以回收ByteBuffer对象,最终系统调用freemermory()方法释放内存。System.gc()会引起一次Full GC,通常比较耗时,影响程序执行。

(2)调用Unsafe类中的freemermory()方法释放内存。可以通过参数-XX:MaxDirectMemorySize来指定直接内存的最大值。若不设置-XX:MaxDirectMemorySize参数,其默认值与-Xmx参数配置的堆内存的最大值一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值