java多个IO流性能PK——MappedByteBuffer问鼎

目录

一、前言

二、效果图

1、文件(1G)读取效率对比

 2、文件(1G)写入效率对比

三、测试代码

1、文件读取代码

2、文件写入代码

四、IO监控

1、pidstat磁盘监控

2、Linux缓存

3、MappedByteBuffer

4、缓存清理验证

五、总结分析

1、性能分析

2、注意事项


一、前言

开篇提醒:本文涉及知识点较多,篇幅较长。

环境准备:在Mac中创建Linux虚拟机
系统版本:centos7.9(aarch64)

处理器:    4核
系统内存:8G
磁盘大小:128G

二、效果图

1、文件(1G)读取效率对比

先上一张效果图,该效果图从每次读取1G文件、写入1G文件。

并从32字节开始,递增至 8M进行对比。

 数据信息:

数据分析:

从读取的数据信息中可以看到,在缓存字节4K之前,MappedByteBuffer占据优势,字节数越少优势越明显,缓冲字节在4K之后,不分伯仲。

注意:

这里引入bufferedInputStream的目的,仅仅是作为“干扰项”,是为了说明其对比意义不大,避免后续对比时引起争议。

bufferedInputStream默认有8K缓冲字节,可认为整个测试过程中,虽然效率上MappedByteBuffer不分伯仲,缓冲字节都为8K,因此不具备可比性。

 2、文件(1G)写入效率对比

 数据信息:

数据分析:

与读取相同, 在缓存字节4K之前,MappedByteBuffer占据优势,字节数越少优势越明显,缓冲字节在4K之后,与普通IO(即fileOutputStream)不分伯仲。

三、测试代码

1、文件读取代码

温馨提醒:博主在测试时,事先准备了需要读取的测试文件,

文件大小:1024*1024*1024 字节,如下图

TestRead.java代码: 

package com.zhufeng.io;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @ClassName: TestRead
 * @Description 文件读取测试
 * @author 月夜烛峰
 * @date 2022/8/23 13:45
 */
public class TestRead {

    private static String FILE_PATH = "/data/zhufeng/io/";

    /**数组内容大小*/
    static int[] contentBytes = {32,64, 128, 512, 1024, 2048, 4096, 8192, 16384, 1048576, 4194304, 8388608};
    static String[] contentUnit = {"32 byte","64 byte", "128  byte", "512  byte", "1 k", "2 k", "4 k", "8 k", "16 k", "1 M" ,"4 M", "8 M"};
    /**文件大小*/
    static long fileSize = 1024 * 1024 * 1024;
    /**读取字节长度*/
    static int DEFAULT_LENGTH = 0;

    /**
     * fileInputStream
     */
    public static void fileInputStream(String name) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(FILE_PATH + name);
            byte[] tempContent = new byte[DEFAULT_LENGTH];
            int readCount = 0;
            long startTime = System.currentTimeMillis();
            while ((readCount = fileInputStream.read(tempContent)) != -1) {
               // String text = new String(tempContent, StandardCharsets.UTF_8);
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("fileInputStream cost :" + interval);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void bufferedInputStream(String name) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(FILE_PATH + name);
            BufferedInputStream bis=new BufferedInputStream(fileInputStream);
            byte[] tempContent = new byte[DEFAULT_LENGTH];
            int readCount = 0;
            long startTime = System.currentTimeMillis();
            while ((readCount = bis.read(tempContent)) != -1) {
               // String text = new String(tempContent, StandardCharsets.UTF_8);
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("bufferedInputStream cost :" + interval);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void randomAccessFile(String name) {
        File file = new File(FILE_PATH + name);
        RandomAccessFile ra;
        try {
            ra = new RandomAccessFile(file, "r");
            long startTime = System.currentTimeMillis();
            while (true) {
                byte[] arr = new byte[DEFAULT_LENGTH];
                int len = ra.read(arr);
                if (len == -1) {
                    break;
                }
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("randomAccessFile cost :" + interval);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * fileChannel
     */
    public static void fileChannel(String name) {
        try {
            RandomAccessFile aFile = new RandomAccessFile(FILE_PATH + name, "r");
            FileChannel inChannel = aFile.getChannel();
            byte[] bytes = new byte[DEFAULT_LENGTH];
            ByteBuffer buf = ByteBuffer.allocate(DEFAULT_LENGTH);
            long startTime = System.currentTimeMillis();
            //read into buffer.
            int bytesRead = inChannel.read(buf);
            while (bytesRead != -1) {
                //make buffer ready for read
                buf.flip();
                while(buf.hasRemaining()){
                    buf.get(bytes);
                }
                buf.clear(); //make buffer ready for writing
                bytesRead = inChannel.read(buf);
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("fileChannel cost :" + interval);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * mappedByteBuffer
     */
    public static void mappedByteBuffer(String name) {

        try {
            FileChannel fc = FileChannel.open(Paths.get(FILE_PATH + name),
                    StandardOpenOption.READ, StandardOpenOption.WRITE);
            byte[] bytes = new byte[DEFAULT_LENGTH];
            //  操作系统提供的一个内存映射的机制的类
            MappedByteBuffer map = fc.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
            long startTime = System.currentTimeMillis();
            while (map.hasRemaining()) {
                map.get(bytes);
                bytes = new byte[DEFAULT_LENGTH];
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("MappedByteBuffer cost :" + interval);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {


        Thread.sleep(20000);

        for(int i=0;i<contentBytes.length;i++){

            DEFAULT_LENGTH = contentBytes[i];

            System.out.println();
            System.out.println("*********开始读取***** "+i+" --- "+contentUnit[i]+" ********");
            System.out.println();
            fileInputStream(i+"-0");
            Thread.sleep(3000);
            bufferedInputStream(i+"-1");
            Thread.sleep(3000);
            randomAccessFile(i+"-2");
            Thread.sleep(3000);
            fileChannel(i+"-3");
            Thread.sleep(3000);
            mappedByteBuffer(1+"-4");
            Thread.sleep(3000);
        }

    }

}

2、文件写入代码

温馨提示:

       a)为了避免文件冗余占用空间,每次循环后都会删除上一循环生成的文件

       b)未避免缓存影响,每次生成时都会随机生成文件名

TestWrite.java代码:

package com.zhufeng.io;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @ClassName: TestWrite
 * @Description IO写测试
 * @author 月夜烛峰
 * @date 2022/8/23 13:45
 */
public class TestWrite {


    private static String FILE_PATH = "/data/zhufeng/io/";
    /**数组内容大小*/
    static int[] contentBytes = {32,64, 128, 512, 1024, 2048, 4096, 8192, 16384, 1048576, 4194304, 8388608};
    static String[] contentUnit = {"32","64 byte", "128  byte", "512  byte", "1 k", "2 k", "4 k", "8 k", "16 k", "1 M" ,"4 M", "8 M"};
    /**生成文件大小*/
    static long fileSize = 1024 * 1024 * 1024;
    /**测试内容*/
    static String content = "IORW读写测试";
    /**写入次数*/
    private static int totals = -1;

    /**
     * FileOutputStream
     */
    public static void fileOutputStream() {
        try {
            File file = getRandomFile();
            FileOutputStream fos = new FileOutputStream(file);
            byte[] tempContent = content.getBytes(StandardCharsets.UTF_8);
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < totals; i++) {
                fos.write(tempContent);
            }
            fos.close();
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("fileOutputStream cost :" + interval);
           // file.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * bufferedWriter
     */
    public static void bufferedWriter() {
        try{
            File file = getRandomFile();
            BufferedWriter bufferedWriter =
                    new BufferedWriter(
                            new OutputStreamWriter(
                                    new FileOutputStream(file),
                                    "UTF-8"));
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < totals; i++) {
                bufferedWriter.write(content);
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("bufferedWriter cost :" + interval);
           // file.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * BufferedOutputStream
     */
    public static void bufferedOutputStream() {
        try {
            File file = getRandomFile();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            byte[] tempContent = content.getBytes(StandardCharsets.UTF_8);
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < totals; i++) {
                bufferedOutputStream.write(tempContent);
            }
            bufferedOutputStream.close();
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("bufferedOutputStream cost :" + interval);
            //file.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * FileWriter
     */
    public static void fileWriter() {
        File f = getRandomFile();
        FileWriter fw = null;
        try {
            //true表示可以追加新内容
            fw = new FileWriter(f.getAbsoluteFile(), true);
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < totals; i++) {
                fw.write(content);
            }
            fw.close();
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("fileWriter cost :" + interval);
           // f.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * RandomAccessFile
     */
    public static void randomAccessFile() {
        try {
            File file = getRandomFile();
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            byte[] tempContent = content.getBytes(StandardCharsets.UTF_8);
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < totals; i++) {
                raf.write(tempContent);
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("randomAccessFile cost :" + interval);
           // file.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * FileChannel
     */
    public static void fileChannel() {

        try {
            File file = getRandomFile();
            FileChannel fc = new RandomAccessFile(file, "rw").getChannel();
            byte[] tempContent = content.getBytes(StandardCharsets.UTF_8);
            ByteBuffer buffer = ByteBuffer.wrap(tempContent);

            long startTime = System.currentTimeMillis();
            buffer.flip();
            for (int i = 0; i < totals; i++) {
                buffer.clear();  //
                fc.write(buffer);
                buffer.flip();
            }
            fc.close();

            long interval = System.currentTimeMillis() - startTime;
            System.out.println("FileChannel cost :" + interval);
            //file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * MappedByteBuffer
     */
    public static void mappedByteBuffer() {
        try {
            File file = getRandomFile();
            MappedByteBuffer mbb = new RandomAccessFile(file, "rw").getChannel().
                    map(FileChannel.MapMode.READ_WRITE, 0, fileSize);
            byte[] tempContent = content.getBytes(StandardCharsets.UTF_8);
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < totals; i++) {
                mbb.put(tempContent);
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("MappedByteBuffer cost :" + interval);
            //file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成随机文件,避免pageCache
     * @return
     */
    private static File getRandomFile() {
        String fileName = FILE_PATH + UUID.randomUUID().toString();
        return new File(fileName);
    }

    public static void main(String[] args) throws Exception {

        Thread.sleep(20000);

        for(int i=0;i<contentBytes.length;i++){
            StringBuilder sbCont = new StringBuilder();
            //拼接每次写入内容
            for(int n=0;n<(contentBytes[i]/content.getBytes(StandardCharsets.UTF_8).length);n++){
                sbCont.append(content);
            }
            content = sbCont.toString();
            //计算循环次数,控制文件大小为1G
            totals = (int) (fileSize/content.getBytes(StandardCharsets.UTF_8).length);

            File tempList = new File(FILE_PATH);


            for(File f:tempList.listFiles()){
                if(f.isDirectory()){
                    continue;
                }
                f.delete();
            }

            for(int x=0;x<5;x++){
                File temp = new File(FILE_PATH+i+"-"+x);
                temp.createNewFile();
            }
            Thread.sleep(1000);

            System.out.println();
            System.out.println("*********开始写入***** "+i+" --- "+contentBytes[i]+" ********");
            System.out.println();

            fileOutputStream();
            Thread.sleep(3000);
            bufferedWriter();
            Thread.sleep(3000);
            bufferedOutputStream();
            Thread.sleep(3000);
            fileWriter();
            Thread.sleep(3000);
            randomAccessFile();
            Thread.sleep(3000);
            fileChannel();
            Thread.sleep(3000);
            mappedByteBuffer();
            Thread.sleep(3000);
        }

    }
}

四、IO监控

先通过fileInputStream了解正常情况下IO读写如何监控以及现象,然后对比MappedByteBuffer有哪些不同。

1、pidstat磁盘监控

测试读取效率时,首先关注的是从磁盘读取效率,以fileInputStream缓冲32字节为例,每秒读取90M左右。

[root@localhost ~]# pidstat -d 1 -p 7085
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

03时11分12秒   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
03时11分21秒     0      7085      0.00      0.00      0.00  java
03时11分22秒     0      7085  36324.75      0.00      0.00  java
03时11分23秒     0      7085  93275.25      0.00      0.00  java
03时11分24秒     0      7085  94208.00      0.00      0.00  java
03时11分25秒     0      7085  94208.00      0.00      0.00  java
03时11分26秒     0      7085  89219.80      0.00      0.00  java
03时11分27秒     0      7085  90112.00      0.00      0.00  java
03时11分28秒     0      7085  93275.25      0.00      0.00  java
03时11分29秒     0      7085  90112.00      0.00      0.00  java
03时11分30秒     0      7085  86016.00      0.00      0.00  java
03时11分31秒     0      7085  90112.00      0.00      0.00  java
03时11分32秒     0      7085  89219.80      0.00      0.00  java
03时11分33秒     0      7085  90112.00      0.00      0.00  java
03时11分34秒     0      7085   8285.15      0.00      0.00  java
03时11分35秒     0      7085      0.00      0.00      0.00  java
03时11分36秒     0      7085      0.00      7.92      0.00  java

每秒写入在55M左右

[root@localhost ~]# pidstat -d 1 -p 2376
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

14时40分53秒   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
14时41分08秒     0      2376      0.00      0.00      0.00  java
14时41分09秒     0      2376   1077.23  51651.49      0.00  java
14时41分10秒     0      2376      0.00  56400.00      0.00  java
14时41分11秒     0      2376      4.00  56080.00      0.00  java
14时41分12秒     0      2376      0.00  56376.00      0.00  java
14时41分13秒     0      2376      4.00  54896.00      0.00  java
14时41分14秒     0      2376      0.00  56596.00      0.00  java
14时41分15秒     0      2376      4.00  56372.00      0.00  java
14时41分16秒     0      2376      0.00  55794.06      0.00  java
14时41分17秒     0      2376      4.00  55576.00      0.00  java
14时41分18秒     0      2376      0.00  54708.00      0.00  java
14时41分19秒     0      2376      0.00  56648.00      0.00  java
14时41分20秒     0      2376      4.00  57072.00      0.00  java
14时41分21秒     0      2376      0.00  55683.17      0.00  java
14时41分22秒     0      2376      4.00  56324.00      0.00  java
14时41分23秒     0      2376      0.00  55308.00      0.00  java
14时41分24秒     0      2376      4.00  55888.00      0.00  java
14时41分25秒     0      2376      0.00  56484.00      0.00  java
14时41分26秒     0      2376      3.96  55564.36      0.00  java
14时41分27秒     0      2376      0.00  42980.00      0.00  java
14时41分28秒     0      2376      0.00      0.00      0.00  java
14时41分29秒     0      2376      0.00      0.00      0.00  java

以读取为例继续分析。

如果当fileInputStream以缓冲32字节第一次读取了某一文件(比如a.txt),当第二次读取a.txt文件时,通过pidstat监控,发现读取都是0,但是fileInputStream确实读取到了a.txt里面的内容。

[root@localhost ~]# pidstat -d 1 -p 7153
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

03时13分55秒   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
03时13分56秒     0      7153      0.00      0.00      0.00  java
03时13分57秒     0      7153      0.00      0.00      0.00  java
03时13分58秒     0      7153      0.00      0.00      0.00  java
03时13分59秒     0      7153      0.00      0.00      0.00  java
03时14分00秒     0      7153      0.00      0.00      0.00  java
03时14分01秒     0      7153      0.00      0.00      0.00  java
03时14分02秒     0      7153      0.00      0.00      0.00  java
03时14分03秒     0      7153      0.00      0.00      0.00  java
03时14分04秒     0      7153      0.00      0.00      0.00  java
03时14分05秒     0      7153      0.00      0.00      0.00  java
03时14分06秒     0      7153      0.00      0.00      0.00  java
03时14分07秒     0      7153      0.00      0.00      0.00  java
03时14分08秒     0      7153      0.00      0.00      0.00  java
03时14分09秒     0      7153      0.00      0.00      0.00  java
03时14分10秒     0      7153      0.00      0.00      0.00  java

这是因为第一次读取a.txt里的内容放到的页缓存page cache中。

2、Linux缓存

通过cat /proc/meminfo可查看系统缓存信息

上图左侧为IO读写前的初始状态,右侧为 fileInputStream第一次读取文件后状态。

其中MemFree内存空闲右侧降低了近1G,Buffers略微增加,Cached增加最为明显,也就是文件内容被缓存了起来,fileInputStream读取文件内容时,如果页缓存已经存在,直接从缓存中读取,减少了磁盘IO交互次数。

再来看MappedByteBuffer有何不同。

3、MappedByteBuffer

看通过pidstat看下MappedByteBuffer以32缓冲字节读取1G文件时,读取效果:

[root@localhost ~]# pidstat -d 1 -p 6841
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

02时57分02秒   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
02时57分16秒     0      6841      0.00      0.00      0.00  java
02时57分17秒     0      6841      0.00      0.00      0.00  java
02时57分18秒     0      6841    142.57     11.88      0.00  java
02时57分19秒     0      6841 1049956.00      8.00      0.00  java
02时57分20秒     0      6841      0.00      0.00      0.00  java
02时57分21秒     0      6841      0.00      0.00      0.00  java

可以看出MappedByteBuffer读取速度在1G左右,相当快!

在监控下内存:

[root@localhost io]# pidstat -p 6841 -r 1
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

02时56分56秒   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
02时57分16秒     0      6841      0.00      0.00 4245552  27980   0.37  java
02时57分17秒     0      6841      0.00      2.00 4245552  27980   0.37  java
02时57分18秒     0      6841 110234.00    263.00 5305660 1460636  19.22  java
02时57分19秒     0      6841      0.00      0.00 5305660 1460636  19.22  java
02时57分20秒     0      6841      0.00      0.00 5305660 1460636  19.22  java

栏位说明:

  • minflt/s:从内存中加载数据时每秒出现的次要错误的数目,不要求从磁盘载入内存界面
  • majflt/s:从内存中加载数据时每秒出现的主要错误的数目,需要从磁盘载入内存界面,一般在内存使用紧张时产生
  • VSZ:占用的虚拟内存大小,包括进入交换分区的内存
  • RSS:占用的物理内存大小,不包括进入交换分区的内
  • %MEN:进程使用的物理内存百分比

minflt/s高,说明没有从磁盘中读取,VSZ增加,说明磁盘中数据加载到了虚拟内存中,需要注意的是,这里虚拟内存是指系统的,并非JVM。

MappedByteBuffer之所以效率高,本质上就是直接读取系统虚拟内存中的数据,并没有将数据从虚拟内存copy到JVM的内存。

对比下fileInputStream内存使用情况

[root@localhost io]# pidstat -p 7153 -r 1
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

03时13分58秒   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
03时13分59秒     0      7153      0.00      0.00 4245552  27580   0.36  java
03时14分00秒     0      7153      0.00      0.00 4245552  27580   0.36  java
03时14分01秒     0      7153      0.00      0.00 4245552  27580   0.36  java
03时14分02秒     0      7153      0.00      0.00 4245552  27580   0.36  java
03时14分03秒     0      7153      0.00      0.00 4245552  27580   0.36  java
03时14分04秒     0      7153      0.00      0.00 4245552  27580   0.36  java
03时14分05秒     0      7153      0.00      0.00 4245552  27580   0.36  java
03时14分06秒     0      7153    108.91      0.00 4245552  28460   0.37  java
03时14分07秒     0      7153      0.00      0.00 4245552  28460   0.37  java
03时14分08秒     0      7153      0.00      0.00 4245552  28460   0.37  java
03时14分09秒     0      7153      0.00      0.00 4245552  28460   0.37  java
03时14分10秒     0      7153      0.00      0.00 4245552  28460   0.37  java

以fileInputStream为代表的普通IO流则需要从磁盘读取数据加载到系统缓存,JVM再将系统缓存中数据copy一份到JVM,相当于多走了一层。

4、缓存清理验证

为了证明我们的“猜想”,可以手动清理page cache来观察fileInputStream和MappedByteBuffer的读取效果。

fileInputStream从32字节到8M重复读取a.txt文件,运行过程中,清理缓存

清理命令:

sync && echo 1 > /proc/sys/vm/drop_caches

sync && echo 2 > /proc/sys/vm/drop_caches

sync && echo 3 > /proc/sys/vm/drop_caches

说明:

值为1时表示可以释放pagecache缓存
值为2时可以释放pagecache和inode缓存
值为3时可以释放pagecache, dentries和inodes缓存

[root@localhost ~]# pidstat -d 1 -p 7869
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

04时02分31秒   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
04时02分36秒     0      7869      0.00      0.00      0.00  java
04时02分37秒     0      7869      0.00      0.00      0.00  java
04时02分38秒     0      7869      0.00      0.00      0.00  java
04时02分39秒     0      7869   4060.00      0.00      0.00  java
04时02分40秒     0      7869  94304.00      0.00      0.00  java
04时02分41秒     0      7869  93366.34      0.00      0.00  java
04时02分42秒     0      7869  94300.00      0.00      0.00  java
04时02分43秒     0      7869  93366.34      0.00      0.00  java
04时02分44秒     0      7869  93366.34      0.00      0.00  java
04时02分45秒     0      7869  94300.00      0.00      0.00  java
04时02分46秒     0      7869  92450.98      0.00      0.00  java
04时02分47秒     0      7869  94300.00      0.00      0.00  java
04时02分48秒     0      7869  90200.00      0.00      0.00  java
04时02分49秒     0      7869  93366.34      0.00      0.00  java
04时02分50秒     0      7869  97425.74      0.00      0.00  java
04时02分51秒     0      7869   8293.07      0.00      0.00  java
04时02分52秒     0      7869      0.00      0.00      0.00  java
04时02分53秒     0      7869      0.00      7.92      0.00  java
04时02分54秒     0      7869      0.00      7.92      0.00  java
04时02分55秒     0      7869      0.00      0.00      0.00  java
04时02分56秒     0      7869      0.00      0.00      0.00  java
04时02分57秒     0      7869      0.00      0.00      0.00  java
04时02分58秒     0      7869      0.00      0.00      0.00  java
04时02分59秒     0      7869      0.00      0.00      0.00  java
04时03分00秒     0      7869      0.00      0.00      0.00  java
04时03分01秒     0      7869      0.00      0.00      0.00  java
04时03分02秒     0      7869      0.00      0.00      0.00  java
04时03分03秒     0      7869      0.00      0.00      0.00  java
04时03分04秒     0      7869      0.00      0.00      0.00  java
04时03分05秒     0      7869      0.00      0.00      0.00  java
04时03分06秒     0      7869      0.00      0.00      0.00  java
04时03分07秒     0      7869      0.00      0.00      0.00  java
04时03分08秒     0      7869      0.00      0.00      0.00  java
04时03分09秒     0      7869      0.00      0.00      0.00  java
04时03分10秒     0      7869      0.00      0.00      0.00  java
04时03分11秒     0      7869      0.00      0.00      0.00  java
04时03分12秒     0      7869      0.00      0.00      0.00  java
04时03分13秒     0      7869      0.00      0.00      0.00  java
04时03分14秒     0      7869      0.00      0.00      0.00  java
04时03分15秒     0      7869  16208.00      8.00      0.00  java
04时03分16秒     0      7869 1012125.49      0.00      0.00  java
04时03分17秒     0      7869      0.00      0.00      0.00  java
04时03分18秒     0      7869      0.00      0.00      0.00  java
04时03分19秒     0      7869      0.00      4.00      0.00  java
04时03分20秒     0      7869      0.00      0.00      0.00  java

根据监控可知,在第一次读取完成后,后续再次读取a.txt时,不再从磁盘读取,所以这里读取速度为0。当执行 sync && echo 1 > /proc/sys/vm/drop_caches 命令后,缓存被清理,又开始从磁盘读取数据。

通过free -m -s 1监控当前内存使用情况也可以看出,buff/cache在执行一段时间后由1117M将为173M。

[root@localhost ~]# free -m -s 1
              total        used        free      shared  buff/cache   available
Mem:           7421         144        7186          11          90        7141
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        7119          11         158        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        7027          11         250        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6935          11         342        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6843          11         434        7115
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6746          11         531        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6654          11         623        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6562          11         715        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6469          11         807        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6377          11         900        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6285          11         992        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6192          11        1084        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6160          11        1117        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6160          11        1117        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         143        6160          11        1117        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         145        6159          11        1117        7112
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         145        7102          11         173        7113
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         144        6923          11         353        7114
Swap:          2095           0        2095

              total        used        free      shared  buff/cache   available
Mem:           7421         144        6739          11         537        7114
Swap:          2095           0        2095
              total        used        free      shared  buff/cache   available
Mem:           7421         144        6558          11         718        7114

再看MappedByteBuffer!

通过磁盘监控,只有在第一次读取文件时读取了磁盘,后面重复读取a.txt文件时,开始清理缓存,但依然没有从磁盘读取数据,通过free监控,缓存中数据也并没有被清空。

[root@localhost ~]# pidstat -d 1 -p 8492
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

06时29分28秒   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
06时29分36秒     0      8492      0.00      0.00      0.00  java
06时29分37秒     0      8492      0.00      0.00      0.00  java
06时29分38秒     0      8492      0.00      0.00      0.00  java
06时29分39秒     0      8492 926556.00      0.00      0.00  java
06时29分40秒     0      8492 127100.00      0.00      0.00  java
06时29分41秒     0      8492      0.00      0.00      0.00  java
06时29分42秒     0      8492      0.00      0.00      0.00  java
06时29分43秒     0      8492      0.00      0.00      0.00  java
06时29分44秒     0      8492      0.00      0.00      0.00  java
06时29分45秒     0      8492      0.00      0.00      0.00  java
06时29分46秒     0      8492      0.00      0.00      0.00  java

虚拟内存继续增加,内存使用率也不断升高,执行清理页缓存对MappedByteBuffer读取效率基本没有影响。

[root@localhost io]# pidstat -p 8492 -r 1
Linux 5.11.12-300.el7.aarch64 (localhost.localdomain) 	2022年08月25日 	_aarch64_	(4 CPU)

06时29分24秒   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
06时29分36秒     0      8492      0.00      0.00 4245552  30864   0.41  java
06时29分37秒     0      8492      0.00      0.00 4245552  30864   0.41  java
06时29分38秒     0      8492  14782.00    221.00 5305660 1285836  16.92  java
06时29分39秒     0      8492   2586.00     39.00 5305660 1468136  19.32  java
06时29分41秒     0      8492      0.00      0.00 5305660 1468136  19.32  java
06时29分42秒     0      8492      0.00      0.00 5305660 1468136  19.32  java
06时29分43秒     0      8492  16613.86      0.00 6354236 2518824  33.15  java
06时29分44秒     0      8492      0.00      0.00 6354236 2518180  33.14  java
06时29分45秒     0      8492      0.00      0.00 6354236 2518180  33.14  java
06时29分46秒     0      8492  15906.80      0.00 7402812 3565220  46.91  java
06时29分47秒     0      8492      0.00      0.00 7402812 3565220  46.91  java
06时29分48秒     0      8492      0.00      0.00 7402812 3565220  46.91  java
06时29分49秒     0      8492  16384.00      0.00 8451388 4612772  60.70  java
06时29分50秒     0      8492      0.00      2.00 8451388 4612772  60.70  java
06时29分51秒     0      8492      0.00      0.00 8451388 4612772  60.70  java
06时29分52秒     0      8492  16222.77      1.98 9499964 5664548  74.54  java
06时29分53秒     0      8492      1.98      0.00 9499964 5664548  74.54  java
06时29分54秒     0      8492      0.00      0.00 9499964 5664548  74.54  java
06时29分55秒     0      8492  16223.76      1.98 10548540 6712100  88.32  java
06时29分56秒     0      8492      0.00      4.00 10548540 6712100  88.32  java
06时29分57秒     0      8492      0.00      4.00 10548540 6712100  88.32  java
06时29分58秒     0      8492  16384.00      2.00 11597116 7759652 102.11  java
06时29分59秒     0      8492      0.00      1.98 11597116 7759652 102.11  java
06时30分00秒     0      8492      0.00      0.00 11597116 7759652 102.11  java
06时30分01秒     0      8492   7087.00      2.00 12645692 8211620 108.06  java
06时30分02秒     0      8492   9297.00      0.00 12645692 8807204 115.89  java
06时30分03秒     0      8492      0.00      0.00 12645692 8807204 115.89  java
06时30分04秒     0      8492      0.00      0.00 12645692 8807204 115.89  java
06时30分05秒     0      8492  16384.00      1.00 13694268 9858980 129.73  java
06时30分06秒     0      8492      0.00      0.00 13694268 9858980 129.73  java
06时30分07秒     0      8492      0.00      0.00 13694268 9858980 129.73  java
06时30分08秒     0      8492  16221.78      0.00 14742844 10906532 143.52  java
06时30分09秒     0      8492      0.00      0.00 14742844 10906532 143.52  java
06时30分10秒     0      8492      0.00      0.00 14742844 10906532 143.52  java
06时30分11秒     0      8492  16082.35      0.00 15791420 11966372 157.46  java
06时30分12秒     0      8492      0.00      0.00 15791420 11966372 157.46  java
06时30分13秒     0      8492      0.00      0.00 15791420 11966372 157.46  java
06时30分14秒     0      8492  16277.23      0.00 16839996 13122084 172.67  java
06时30分15秒     0      8492      0.00      0.00 16839996 13122084 172.67  java
06时30分16秒     0      8492      0.00      0.00 16839996 13122084 172.67  java
[root@localhost io]#

五、总结分析

1、性能分析

从代码层面上看,从硬盘上将文件读入内存,都要经过文件系统进行数据拷贝,并且数据拷贝操作是由文件系统和硬件驱动实现的,理论上来说,拷贝数据的效率是一样的。

但是通过内存映射的方法访问硬盘上的文件,效率要比read和write系统调用高,这是为什么?
read()是系统调用,首先将文件从硬盘拷贝到内核空间的一个缓冲区,再将这些数据拷贝到用户空间,实际上进行了两次数据拷贝;

map()也是系统调用,但没有进行数据拷贝,当缺页中断发生时,直接将文件从硬盘拷贝到用户空间,只进行了一次数据拷贝。

所以,采用内存映射的读写效率要比传统的read/write性能高。

2、注意事项

MappedByteBuffer使用虚拟内存,因此分配(map)的内存大小不受JVM的-Xmx参数限制,但是也是有大小限制的。

如果当文件超出1.5G()限制时,可以通过position参数重新map文件后面的内容。

 MappedByteBuffer在处理大文件时的确性能很高,但也存在一些问题,如内存占用、文件关闭不确定,被其打开的文件只有在垃圾回收的才会被关闭,而且这个时间点是不确定的。

javadoc中也提到:A mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected.*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月夜烛峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值