国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

Netty之ByteBuf 分配

2018-08-08 10:34 更新

本節(jié)介紹 ByteBuf 實(shí)例管理的幾種方式:

ByteBufAllocator

為了減少分配和釋放內(nèi)存的開銷,Netty 通過(guò)支持池類 ByteBufAllocator,可用于分配的任何 ByteBuf 我們已經(jīng)描述過(guò)的類型的實(shí)例。是否使用池是由應(yīng)用程序決定的,表5.8列出了 ByteBufAllocator 提供的操作。

Table 5.8 ByteBufAllocator methods

名稱描述
buffer() buffer(int) buffer(int, int)Return a ByteBuf with heap-based or direct data storage.
heapBuffer() heapBuffer(int) heapBuffer(int, int)Return a ByteBuf with heap-based storage.
directBuffer() directBuffer(int) directBuffer(int, int)Return a ByteBuf with direct storage.
compositeBuffer() compositeBuffer(int) heapCompositeBuffer() heapCompositeBuffer(int) directCompositeBuffer()directCompositeBuffer(int)Return a CompositeByteBuf that can be expanded by adding heapbased or direct buffers.
ioBuffer()Return a ByteBuf that will be used for I/O operations on a socket.

通過(guò)一些方法接受整型參數(shù)允許用戶指定 ByteBuf 的初始和最大容量值。你可能還記得,ByteBuf 存儲(chǔ)可以擴(kuò)大到其最大容量。

得到一個(gè) ByteBufAllocator 的引用很簡(jiǎn)單。你可以得到從 Channel (在理論上,每 Channel 可具有不同的 ByteBufAllocator ),或通過(guò)綁定到的 ChannelHandler 的 ChannelHandlerContext 得到它,用它實(shí)現(xiàn)了你數(shù)據(jù)處理邏輯。

下面的列表說(shuō)明獲得 ByteBufAllocator 的兩種方式。

Listing 5.15 Obtain ByteBufAllocator reference

Channel channel = ...;
ByteBufAllocator allocator = channel.alloc(); //1
....
ChannelHandlerContext ctx = ...;
ByteBufAllocator allocator2 = ctx.alloc(); //2
...

1.從 channel 獲得 ByteBufAllocator

2.從 ChannelHandlerContext 獲得 ByteBufAllocator

Netty 提供了兩種 ByteBufAllocator 的實(shí)現(xiàn),一種是 PooledByteBufAllocator,用ByteBuf 實(shí)例池改進(jìn)性能以及內(nèi)存使用降到最低,此實(shí)現(xiàn)使用一個(gè)“jemalloc”內(nèi)存分配。其他的實(shí)現(xiàn)不池化 ByteBuf 情況下,每次返回一個(gè)新的實(shí)例。

Netty 默認(rèn)使用 PooledByteBufAllocator,我們可以通過(guò) ChannelConfig 或通過(guò)引導(dǎo)設(shè)置一個(gè)不同的實(shí)現(xiàn)來(lái)改變。更多細(xì)節(jié)在后面講述 ,見 Chapter 9, "Bootstrapping Netty Applications"

Unpooled (非池化)緩存

當(dāng)未引用 ByteBufAllocator 時(shí),上面的方法無(wú)法訪問(wèn)到 ByteBuf。對(duì)于這個(gè)用例 Netty 提供一個(gè)實(shí)用工具類稱為 Unpooled,,它提供了靜態(tài)輔助方法來(lái)創(chuàng)建非池化的 ByteBuf 實(shí)例。表5.9列出了最重要的方法

Table 5.9 Unpooled helper class

名稱描述
buffer() buffer(int) buffer(int, int)Returns an unpooled ByteBuf with heap-based storage
directBuffer() directBuffer(int) directBuffer(int, int)Returns an unpooled ByteBuf with direct storage
wrappedBuffer()Returns a ByteBuf, which wraps the given data.
copiedBuffer()Returns a ByteBuf, which copies the given data

在 非聯(lián)網(wǎng)項(xiàng)目,該 Unpooled 類也使得它更容易使用的 ByteBuf API,獲得一個(gè)高性能的可擴(kuò)展緩沖 API,而不需要 Netty 的其他部分的。

ByteBufUtil

ByteBufUtil 靜態(tài)輔助方法來(lái)操作 ByteBuf,因?yàn)檫@個(gè) API 是通用的,與使用池?zé)o關(guān),這些方法已經(jīng)在外面的分配類實(shí)現(xiàn)。

也許最有價(jià)值的是 hexDump() 方法,這個(gè)方法返回指定 ByteBuf 中可讀字節(jié)的十六進(jìn)制字符串,可以用于調(diào)試程序時(shí)打印 ByteBuf 的內(nèi)容。一個(gè)典型的用途是記錄一個(gè) ByteBuf 的內(nèi)容進(jìn)行調(diào)試。十六進(jìn)制字符串相比字節(jié)而言對(duì)用戶更友好。 而且十六進(jìn)制版本可以很容易地轉(zhuǎn)換回實(shí)際字節(jié)表示。

另一個(gè)有用方法是 使用 boolean equals(ByteBuf, ByteBuf),用來(lái)比較 ByteBuf 實(shí)例是否相等。在 實(shí)現(xiàn)自己 ByteBuf 的子類時(shí)經(jīng)常用到。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)