Java中ByteBuffer的allocate方法 和allocateDirect方法的區(qū)別和選用原則解析
背景
公司開發(fā)一個商用的支付應用。寫協(xié)議的時候需要用到byte類型數(shù)組來填充協(xié)議的報文數(shù)據(jù)。在調(diào)研了JDK各個類庫之后,最終選用Java類庫中的ByteBuffer。
在Java中,ByteBuffer是java.nio包中的一個類,用于處理字節(jié)數(shù)據(jù)。ByteBuffer提供了兩種方式來分配內(nèi)存:allocate和allocateDirect。
/**
* Allocates a new direct byte buffer.
*
* <p> The new buffer's position will be zero, its limit will be its
* capacity, its mark will be undefined, and each of its elements will be
* initialized to zero. Whether or not it has a
* {@link #hasArray backing array} is unspecified.
*
* @param capacity
* The new buffer's capacity, in bytes
*
* @return The new byte buffer
*
* @throws IllegalArgumentException
* If the <tt>capacity</tt> is a negative integer
*/
public static ByteBuffer allocateDirect(int capacity) {
// Android-changed: Android's DirectByteBuffers carry a MemoryRef.
// return new DirectByteBuffer(capacity);
DirectByteBuffer.MemoryRef memoryRef = new DirectByteBuffer.MemoryRef(capacity);
return new DirectByteBuffer(capacity, memoryRef);
} /**
* Allocates a new byte buffer.
*
* <p> The new buffer's position will be zero, its limit will be its
* capacity, its mark will be undefined, and each of its elements will be
* initialized to zero. It will have a {@link #array backing array},
* and its {@link #arrayOffset array offset} will be zero.
*
* @param capacity
* The new buffer's capacity, in bytes
*
* @return The new byte buffer
*
* @throws IllegalArgumentException
* If the <tt>capacity</tt> is a negative integer
*/
public static ByteBuffer allocate(int capacity) {
if (capacity < 0)
throw new IllegalArgumentException();
return new HeapByteBuffer(capacity, capacity);
}區(qū)別
這兩種方式分別對應于不同的內(nèi)存分配策略,具有不同的優(yōu)劣勢。
1. allocate:
- 使用
allocate方法分配的內(nèi)存是在Java虛擬機的堆內(nèi)存中。 ByteBuffer.allocate(capacity)分配的是非直接緩沖區(qū)(non-direct buffer)。- 非直接緩沖區(qū)的操作會在Java堆內(nèi)存中進行,數(shù)據(jù)的讀寫會通過Java堆來傳遞。
```java ByteBuffer buffer = ByteBuffer.allocate(1024); ```
2. allocateDirect:
- 使用
allocateDirect方法分配的內(nèi)存是在操作系統(tǒng)的本地內(nèi)存中,而不是在Java堆內(nèi)存中。 ByteBuffer.allocateDirect(capacity)分配的是直接緩沖區(qū)(direct buffer)。- 直接緩沖區(qū)的操作可以通過本地I/O傳遞,避免了在Java堆和本地堆之間的數(shù)據(jù)傳輸,可能在某些情況下提供更好的性能。
```java ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024); ```
總結
選擇使用哪種方式取決于應用的需求和性能特征:
- allocate: 適用于較小的緩沖區(qū),對內(nèi)存占用不太敏感的情況。由于是在Java堆上分配,垃圾回收器能夠管理這部分內(nèi)存,但可能會有一些性能開銷。
- allocateDirect: 適用于需要較大緩沖區(qū)或?qū)π阅芤筝^高的情況。由于是在本地內(nèi)存上分配,可能減少了一些垃圾回收器的開銷,但在分配和釋放直接緩沖區(qū)時可能涉及到一些本地資源的操作。
在使用allocateDirect時需要謹慎,因為它可能占用較多的本地內(nèi)存,過度使用可能導致本地內(nèi)存耗盡。
在選用這兩種技術方案中哪一種的時候需要根據(jù)具體的應用場景和需求權衡兩者之間的取舍。
到此這篇關于Java中ByteBuffer的allocate方法 和allocateDirect方法的區(qū)別和選用原則 的文章就介紹到這了,更多相關Java ByteBuffer的allocate方法 和allocateDirect方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot整合Spring?Security實現(xiàn)權限控制的全過程
本文介紹了在企業(yè)級項目中使用SpringSecurity實現(xiàn)權限控制的方法,包括認證與授權的概念、核心數(shù)據(jù)模型、SpringSecurity入門搭建、核心配置、數(shù)據(jù)庫認證與密碼加密、權限控制方式、用戶退出登錄配置等,最后,強調(diào)了生產(chǎn)環(huán)境下的安全配置要求,需要的朋友可以參考下2026-04-04
淺析如何在SpringBoot上傳中將MultipartFile轉File對象
在我們開發(fā) Spring Boot Web 應用中,文件上傳通常通過 MultipartFile 接口接收,但有時我們需要將 MultipartFile 轉成標準的 java.io.File 對象,下面我們就來看看具體實現(xiàn)吧2025-08-08
spring+srpingmvc+hibernate實現(xiàn)動態(tài)ztree生成樹狀圖效果
這篇文章主要介紹了spring+srpingmvc+hibernate動態(tài)ztree生成樹狀圖效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
五分鐘解鎖springboot admin監(jiān)控新技巧
本文不會講如何搭建企業(yè)的運維監(jiān)控系統(tǒng),有興趣的可以去找找成熟的比如Zabbix、Prometheus,甚至比較簡單的Wgcloud都能滿足一定的需求,不在此贅述。本文講解如何使用Springboot admin對spring boot項目進行應用監(jiān)控,感興趣的朋友一起看看吧2021-06-06
java socket大數(shù)據(jù)傳輸丟失問題及解決
這篇文章主要介紹了java socket大數(shù)據(jù)傳輸丟失問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Spring中的SpringApplicationRunListener詳細解析
這篇文章主要介紹了Spring中的SpringApplicationRunListener詳細解析,SpringApplicationRunListener是一個監(jiān)聽SpringApplication中run方法的接口,在項目啟動過程的各個階段進行事件的發(fā)布,需要的朋友可以參考下2023-11-11

