Java直接内存与非直接内存性能测试
什么是直接内存与非直接内存
根据官方文档的描述:
A byte buffer is either direct or non-direct. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.
byte byffer可以是两种类型,一种是基于直接内存(也就是非堆内存);另一种是非直接内存(也就是堆内存)。
对于直接内存来说,JVM将会在IO操作上具有更高的性能,因为它直接作用于本地系统的IO操作。而非直接内存,也就是堆内存中的数据,如果要作IO操作,会先复制到直接内存,再利用本地IO处理。
从数据流的角度,非直接内存是下面这样的作用链:
本地IO-->直接内存-->非直接内存-->直接内存-->本地IO
而直接内存是:
本地IO-->直接内存-->本地IO
很明显,再做IO处理时,比如网络发送大量数据时,直接内存会具有更高的效率。
A direct byte buffer may be created by invoking the allocateDirect factory method of this class. The buffers returned by this method typically have somewhat higher allocation and deallocation costs than non-direct buffers. The contents of direct buffers may reside outside of the normal garbage-collected heap, and so their impact upon the memory footprint of an application might not be obvious. It is therefore recommended that direct buffers be allocated primarily for large, long-lived buffers that are subject to the underlying system's native I/O operations. In general it is best to allocate direct buffers only when they yield a measureable gain in program performance.
但是,不要高兴的太早。文档中也说了,直接内存使用allocateDirect创建,但是它比申请普通的堆内存需要耗费更高的性能。不过,这部分的数据是在JVM之外的,因此它不会占用应用的内存。
所以呢,当你有很大的数据要缓存,并且它的生命周期又很长,那么就比较适合使用直接内存。只是一般来说,如果不是能带来很明显的性能提升,还是推荐直接使用堆内存。
关于直接内存需要注意的,就是上面两点了,其他的关于视图啊、作用链啊,都是使用上的问题了。如果有兴趣,可以参考官方API ( 进去后搜索ByteBuffer,就能看到!),里面有少量的描述!重要的一些用法,还得自己摸索。
使用场景
通过上面的官方文档,与一些资料的搜索。可以总结下,直接内存的使用场景:
- 1 有很大的数据需要存储,它的生命周期又很长
- 2 适合频繁的IO操作,比如网络并发场景
申请分配地址速度比较
下面用一段简单的代码,测试下申请内存空间的速度:
int time = 10000000;
Date begin = new Date();
for(int i=0;i<time;i++){
ByteBuffer buffer = ByteBuffer.allocate(2);
}
Date end = new Date();
System.out.println(end.getTime()-begin.getTime());
begin = new Date();
for(int i=0;i<time;i++){
ByteBuffer buffer = ByteBuffer.allocateDirect(2);
}
end = new Date();
System.out.println(end.getTime()-begin.getTime());
得到的测试结果如下:

在数据量提升时,直接内存相比于非直接内存的申请 有十分十分十分明显的性能问题!
读写速度比较
然后在写段代码,测试下读写的速度:
int time = 1000;
Date begin = new Date();
ByteBuffer buffer = ByteBuffer.allocate(2*time);
for(int i=0;i<time;i++){
buffer.putChar('a');
}
buffer.flip();
for(int i=0;i<time;i++){
buffer.getChar();
}
Date end = new Date();
System.out.println(end.getTime()-begin.getTime());
begin = new Date();
ByteBuffer buffer2 = ByteBuffer.allocateDirect(2*time);
for(int i=0;i<time;i++){
buffer2.putChar('a');
}
buffer2.flip();
for(int i=0;i<time;i++){
buffer2.getChar();
}
end = new Date();
System.out.println(end.getTime()-begin.getTime());
测试的结果如下:

可以看到直接内存在直接的IO操作上,还是有明显的差异的!
Java直接内存与非直接内存性能测试的更多相关文章
- 【转载】java项目中经常碰到的内存溢出问题: java.lang.OutOfMemoryError: PermGen space, 堆内存和非堆内存,写的很好,理解很方便
Tomcat Xms Xmx PermSize MaxPermSize 区别 及 java.lang.OutOfMemoryError: PermGen space 解决 解决方案 在 catalin ...
- 干货:JVM 堆内存和非堆内存
堆和非堆内存 按照官方的说法:"Java 虚拟机具有一个堆(Heap),堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时创建的."" ...
- JVM 堆内存和非堆内存
转载自:http://www.importnew.com/27645.html 堆和非堆内存 按照官方的说法:“Java 虚拟机具有一个堆(Heap),堆是运行时数据区域,所有类实例和数组的内存均从此 ...
- Windows内核编程之:分页内存与非分页内存 #define PAGEDCODE code_seg("PAGE") (转)
原文链接:http://blog.chinaunix.net/uid-24504987-id-161192.html Windows规定有些虚拟内存可以交换到文件中,这类内存被称为分页内存 有些虚拟内 ...
- Windows内核编程之:分页内存与非分页内存
Windows规定有些虚拟内存可以交换到文件中,这类内存被称为分页内存 有些虚拟内存 永远不会交换到文件中,这些内存叫非分页内存 #define PAGEDCODE code_seg(“PAGE”); ...
- 如何让IntPtr指向一块内存,以及托管内存与非托管内存的相互转化
IntPtr idp= IntPtr.Zero; StringBuilder idata = new StringBuilder("000000"); string idata = ...
- C# 托管内存与非托管内存之间的转换
c#有自己的内存回收机制,所以在c#中我们可以只new,不用关心怎样delete,c#使用gc来清理内存,这部分内存就是managed memory,大部分时候我们工作于c#环境中,都是在使用托管内存 ...
- C# 中托管内存与非托管内存之间的转换
c#有自己的内存回收机制,所以在c#中我们可以只new,不用关心怎样delete,c#使用gc来清理内存,这部分内存就是managed memory,大部分时候我们工作于c#环境中,都是在使用托管内存 ...
- Java堆内存Heap与非堆内存Non-Heap
堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时创建的.”“在 ...
随机推荐
- Codeforces 608B. Hamming Distance Sum 模拟
B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- vue-router2.0 初学--动态赋值
A:router路由配置 1 export default new Router({ 2 routes: [ 3 { 4 path: '/home', 5 name: 'Home', 6 compon ...
- swift http post json + 登录
var nsUrl : NSURL = NSURL(string:API_HOST+"/"+LOGIN_API)! var request = NSMutableURLReques ...
- 使用JDBC连接MySql时出现:The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration
在连接字符串后面加上?serverTimezone=UTC 其中UTC是统一标准世界时间. 完整的连接字符串示例:jdbc:mysql://localhost:3306/test?serverTime ...
- 情境领导II
情境领导理论认为,领导者的行为要与被领导者的准备程度相适应,才能取得有效的领导效果,也就是说领导风格不是一成不变的,而要根据环境及员工的变化而改变. 三大技巧分别为诊断.弹性与约定领导型态.诊断是评估 ...
- 2018.10.23 NOIP模拟 “新”的家园(缩图+dijksta/spfa)
传送门 考试70分骗分写挂了=30分=全场最低. 哎今天230垫底了. 这题出的挺好. 对于非关键点直接缩点. 每次把要查的insertinsertinsert进缩好的图里面跑spfa/dijkstr ...
- 2018.07.08 NOIP模拟 第K小数(二分)
第K小数 题目背景 SOURCE:NOIP2016-AHSDFZ T1 题目描述 有两个正整数数列,元素个数分别为 N 和 M .从两个数列中分别任取一个数相乘,这样一共可以得到 N*M 个数,询问这 ...
- redis学习-有序集合(zset)常用命令
zadd:有序集合增加一个或者多个键值对 与set集合不同,zset添加的时候需要 指定 score,这个是用来排名的 zrange:返回指定范围的键 zcount:返回集合指定范围的个数(以每个键值 ...
- 带你快速进入.net core的世界(转)
出处:http://www.cnblogs.com/zhaopei/p/netcore.html 阅读目录 vmware虚拟机安装 CentOS7.3安装 Windows的客户端软件 .NET Cor ...
- Part 6 - Class-Based Views(21-26)
https://github.com/sibtc/django-beginners-guide/tree/v0.6-lw urlpatterns = [ views.PostUpdateView.as ...