歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java直接內存與非直接內存性能測試

Java直接內存與非直接內存性能測試

日期:2017/3/1 9:17:48   编辑:Linux編程

什麼是直接內存與非直接內存

根據官方文檔的描述:

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操作上,還是有明顯的差異的!

Copyright © Linux教程網 All Rights Reserved