直接内存(Direct Memory)牛刀小试

简介: 直接内存(Direct Memory)牛刀小试

Direct Memory

特点

  • 常见于NIO操作时,用于数据缓存
  • 分配回收成本较高,但是读写性能高
  • 不受JVM内存回收管理

案例说明

为了做对比,还是直接写一段程序测试:

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

/**
 * 直接内存的读写测试
 */
public class Jvm1_9 {
    static final String fromFile="/tmp/12GB.dat";
    static final String toFile="/tmp/12GB_new.dat";
    //mkfile -n 12g  /tmp/12GB.dat
    static  final int _256M =256*1024*1024;
    public static void main(String[] args) {
        io();
        directBuffer();
    }
    public static  void ready(String file){
        File   f=new File(file);
        if(f.exists()){
            f.delete();
        }
    }
    public static void io(){
        ready(toFile);
        long start=System.nanoTime();
        try(FileInputStream from =new FileInputStream(fromFile);
            FileOutputStream to=new FileOutputStream((toFile));
        ){
           byte[] buffer=new  byte[_256M];
           while (true){
               int len=from.read(buffer);
              if(len==-1){
                  break;
              }
              to.write(buffer);
           }
        }catch (IOException e){
            e.printStackTrace();
        }
        long end=System.nanoTime();
        System.out.println("io耗时:"+(end-start)/1000_000.0);
    }
    public static void directBuffer(){
        ready(toFile);
        long start=System.nanoTime();
        try(FileChannel from =new FileInputStream(fromFile).getChannel();
            FileChannel to=new FileOutputStream((toFile)).getChannel();
            ){
            ByteBuffer byteBuffer=ByteBuffer.allocateDirect(_256M);
            while (true){
                int len=from.read(byteBuffer);
                if(len==-1){
                    break;
                }
                byteBuffer.flip();
                to.write(byteBuffer);
                byteBuffer.clear();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        long end=System.nanoTime();
        System.out.println("directBuffer耗时:"+(end-start)/1000_000.0);
    }
}

程序说明:

分别使用传统的IO操作和使用direct操作进行对比,分配一样大小的buffer进行操作,然后做耗时对比。运行之前需要创建一个比较大的文件:

mkfile -n 12g  /tmp/12GB.dat

运行结果如下:

io耗时:38111.937769
directBuffer耗时:21497.436392

我们可以看到directbuffer有明显的提升,不过需要说明的是这个操作需要不断测试,而且文件的大小需要上量级,一开始我用来几M文件做测试,效果其实差别不大,主要是两种操作都比较快,不能说明问题。当文件数量达到一定规模之后,这种差异逐渐会稳定下来

目录
相关文章
|
Python
什么是Python中的内存池(Memory Pool)?
什么是Python中的内存池(Memory Pool)?
255 0
|
数据库 数据库管理 Python
解释Python中的内存视图(Memory View)。
解释Python中的内存视图(Memory View)。
557 0
|
9月前
|
Arthas 监控 Java
Arthas memory(查看 JVM 内存信息)
Arthas memory(查看 JVM 内存信息)
755 6
|
8月前
|
Arthas 监控 Java
Arthas mc(Memory Compiler/内存编译器 )
Arthas mc(Memory Compiler/内存编译器 )
274 6
|
存储 缓存 数据安全/隐私保护
DMA(Direct Memory Access):直接内存访问
DMA(Direct Memory Access)是一种允许外设直接与内存进行数据传输的技术,无需 CPU 干预。它通过减轻 CPU 负担、提高数据传输效率来提升系统性能。DMA 的工作模式包括直接模式和 FIFO 模式,数据传输方式有单字传送和块传送,寻址模式有增量寻址和非增量寻址。通过缓存一致性协议、同步机制、数据校验和合理的内存管理,DMA 确保了数据在内存中的一致性和完整性。
|
Rust 编译器
|
存储 网络协议 大数据
一文读懂RDMA: Remote Direct Memory Access(远程直接内存访问)
该文档详细介绍了RDMA(远程直接内存访问)技术的基本原理、主要特点及其编程接口。RDMA通过硬件直接在应用程序间搬移数据,绕过操作系统协议栈,显著提升网络通信效率,尤其适用于高性能计算和大数据处理等场景。文档还提供了RDMA编程接口的概述及示例代码,帮助开发者更好地理解和应用这一技术。
|
设计模式 uml
在电脑主机(MainFrame)中只需要按下主机的开机按钮(on()),即可调用其它硬件设备和软件的启动方法,如内存(Memory)的自检(check())、CPU的运行(run())、硬盘(Hard
该博客文章通过一个电脑主机启动的示例代码,展示了外观模式(Facade Pattern)的设计模式,其中主机(MainFrame)类通过调用内部硬件组件(如内存、CPU、硬盘)和操作系统的启动方法来实现开机流程,同时讨论了外观模式的优缺点。
|
存储 缓存 监控
托管内存(Managed Memory)
托管内存(Managed Memory)
|
监控 安全 Java
JVM内存问题之排查Direct Memory泄漏有哪些常用方法
JVM内存问题之排查Direct Memory泄漏有哪些常用方法
732 2