日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java直接内存与非直接内存性能测试

發布時間:2025/3/21 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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操作上,還是有明顯的差異的!

作者:xingoo

出處:http://www.cnblogs.com/xing901022

from:https://www.cnblogs.com/xing901022/p/5243657.html

總結

以上是生活随笔為你收集整理的Java直接内存与非直接内存性能测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。