1. 程式人生 > >Java直接記憶體與非直接記憶體效能測試

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