1. 概述
JDK8及之后方法区的实现变成了元空间,元空间不再使用JVM内存,而是直接使用系统内存,故称为直接内存。对于元空间来说,它具有如下的特点:
- 元空间不再位于运行时数据区,也不是Java虚拟机规范中定义的区域
- 元空间直接使用系统内存空间
- 访问直接内存的效率更高,读写性能更好
元空间的思想来源于Java中的NIO,它通过堆中的DirectByteBuffer操作本地内存。它允许程序使用直接内存,用于数据缓冲区。
import java.nio.ByteBuffer;import java.util.Scanner;public class BufferTest {private static final int BUFFER = 1024 * 1024 * 1024;//1GBpublic 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();}}
2. 直接缓冲与非直接缓冲
2.1 非直接缓冲
非直接缓冲方式读写文件时,需要与磁盘进行交互。因此,它需要用户态和内核态之间的切换。而用户态和内核态之间的相关切换通常开销较大,因此,使用非直接缓冲方式读写文件性能较低。如下所示:
- 当应用程序想要读取物理磁盘上的数据时,首先需要从用户态切换到内核态,由系统负责数据的读取,将数据读取到内核地址空间中。然后再将内核地址空间中的数据复制到用户地址空间中,这样程序才可以使用
- 当应用程序要往物理磁盘中写入数据时,过程和上面的正好相反,它同样需要进行数据的复制,需要维护两块地址空间来完成写入操作
2.2 直接缓冲
当使用NIO时,操作系统会划出一块区域作为直接缓存区,此时Java程序可以直接访问该缓冲区即可,不必进行状态的切换和数据的复制。
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 String TO = "C:test.txt";private static final int _100Mb = 1024 * 1024 * 100;public static void main(String[] args) {long sum = 0;String src = "C:test.txt";for (int i = 0; i < 3; i++) {String dest = "D:test" + i + ".txt";sum += directBuffer(src,dest);}System.out.println("总花费的时间为:" + sum );}// NIO方式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;}}
3. 直接内存的OOM
虽然直接内存只取决于系统内存的大小情况,但也可能出现OOM。因为Java堆和直接内存的总和依然受限于系统内存的最大值,当堆所占空间太大时,直接内存太小时就可能会出现OOM。
直接内存大小可通过MaxDirectMemorySize参数设置,如果不自定义设置,那么它的大小将和堆的最大值-Xmx参数值一样。
import java.nio.ByteBuffer;import java.util.ArrayList;public class OOMTest {private static final int BUFFER = 1024 * 1024 * 20;//20MBpublic static void main(String[] args) {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);}}}
当直接内存用尽时,程序就会抛出OOM异常
89Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memoryat java.nio.Bits.reserveMemory(Bits.java:694)at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)at DirectBuffer.OOMTest.main(OOMTest.java:14)
4.总结
直接内存使用系统内存,因此使用性能较高。但是它分配回收成本高,而且不受JVM内存回收管理。
