直接内存(堆外内存)与堆内存比较

直接内存(堆外内存)与堆内存比较
  1. 直接内存申请空间耗费更高的性能,当频繁申请到一定量时尤为明显
  2. 直接内存IO读写的性能要优于普通的堆内存,在多次读写操作的情况下差异明显
代码验证:
package com.xnccs.cn.share;
import java.nio.ByteBuffer;

/**
* 直接内存 与 堆内存的比较
*/
public  class  ByteBufferCompare {

public   static   void   main (String[] args) {
allocateCompare();  //分配比较
operateCompare();  //读写比较
}
/**
* 直接内存 和 堆内存的 分配空间比较
*
* 结论:  在数据量提升时,直接内存相比非直接内的申请,有很严重的性能问题
*
*/
public   static   void   allocateCompare (){
int  time =  10000000 //操作次数

long  st = System.currentTimeMillis();
for  ( int  i =  0 ; i < time; i++) {
//ByteBuffer.allocate(int capacity) 分配一个新的字节缓冲区。
ByteBuffer buffer = ByteBuffer.allocate( 2 );  //非直接内存分配申请
}
long  et = System.currentTimeMillis();
System.out.println( "在进行" +time+ "次分配操作时,堆内存 分配耗时:"  + (et-st) + "ms"  );
long  st_heap = System.currentTimeMillis();
for  ( int  i =  0 ; i < time; i++) {
//ByteBuffer.allocateDirect(int capacity) 分配新的直接字节缓冲区。
ByteBuffer buffer = ByteBuffer.allocateDirect( 2 );  //直接内存分配申请
}
long  et_direct = System.currentTimeMillis();
System.out.println( "在进行" +time+ "次分配操作时,直接内存 分配耗时:"  + (et_direct-st_heap) + "ms"  );
}
/**
* 直接内存 和 堆内存的 读写性能比较
*
* 结论: 直接内存在直接的IO 操作上,在频繁的读写时 会有显著的性能提升
*
*/
public   static   void   operateCompare (){
int  time =  1000000000 ;
ByteBuffer buffer = ByteBuffer.allocate( 2 *time);
long  st = System.currentTimeMillis();
for  ( int  i =  0 ; i < time; i++) {
// putChar(char value) 用来写入 char 值的相对 put 方法
buffer.putChar( 'a' );
}
buffer.flip();
for  ( int  i =  0 ; i < time; i++) {
buffer.getChar();
}
long  et = System.currentTimeMillis();
System.out.println( "在进行" +time+ "次读写操作时,非直接内存读写耗时:"  + (et-st) + "ms" );
ByteBuffer buffer_d = ByteBuffer.allocateDirect( 2 *time);
long  st_direct = System.currentTimeMillis();
for  ( int  i =  0 ; i < time; i++) {
// putChar(char value) 用来写入 char 值的相对 put 方法
buffer_d.putChar( 'a' );
}
buffer_d.flip();
for  ( int  i =  0 ; i < time; i++) {
buffer_d.getChar();
}
long  et_direct = System.currentTimeMillis();
System.out.println( "在进行" +time+ "次读写操作时,直接内存读写耗时:"  + (et_direct - st_direct) + "ms" );
}
}
输出: 
在进行10000000次分配操作时,堆内存 分配耗时:12ms 
在进行10000000次分配操作时,直接内存 分配耗时:8233ms 
在进行1000000000次读写操作时,非直接内存读写耗时:4055ms 
在进行1000000000次读写操作时,直接内存读写耗时:745ms
可以自己设置不同的time 值进行比较
分析
从数据流的角度,来看
非直接内存作用链: 
本地IO –>直接内存–>非直接内存–>直接内存–>本地IO 
直接内存作用链: 
本地IO–>直接内存–>本地IO
直接内存使用场景
  • 有很大的数据需要存储,它的生命周期很长
  • 适合频繁的IO操作,例如网络并发场景
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值