本節(jié)開始學(xué)習(xí)netty的內(nèi)存分配機制,搜先是ByteBuf。
作為一個容器,源碼中的如下。有三塊區(qū)域
- discardable bytes:無效空間(已經(jīng)讀取過的空間),可丟棄字節(jié)的區(qū)域,由
readerIndex指針控制 - readable bytes:內(nèi)容空間,可讀字節(jié)的區(qū)域,由
readerIndex和writerIndex指針控制控制 - writable bytes:空閑空間,可寫入字節(jié)的區(qū)域,由
writerIndex指針和capacity容量控制
* <pre>
* +-------------------+------------------+------------------+
* | discardable bytes | readable bytes | writable bytes |
* | | (CONTENT) | |
* +-------------------+------------------+------------------+
* | | | |
* 0 <= readerIndex <= writerIndex <= capacity
* </pre>
-
ByteBuf還定義了抽象方法maxCapacity,用于子類可增加一個最大容量的指針。
public abstract int maxCapacity();
- ByteBuf定義了豐富的api,主要是讀寫和設(shè)置以及一些指針和容量的操作,詳情參考《ByteBuf 主要API》
- 讀邏輯在
AbstractByteBuf骨架類,主要就是操作readerIndex指針,而具體怎么讀著交給具體子類實現(xiàn),暴露_getByte(i);給子類即可。
@Override
public byte readByte() {
checkReadableBytes0(1);
int i = readerIndex;
byte b = _getByte(i);
readerIndex = i + 1;
return b;
}
- 寫邏輯在
AbstractByteBuf骨架類,把當(dāng)前的值寫到ByteBuf里面,擦歐總writerIndex指針,并且暴露_setByte(writerIndex++, value);給子類去具體實現(xiàn)寫邏輯
@Override
public ByteBuf writeByte(int value) {
ensureWritable0(1);
_setByte(writerIndex++, value);
return this;
}
-
ByteBuf分類
ByteBuf家族
- pooled和unpooled
Pooled:每次都從預(yù)先分配好的內(nèi)存中去取出一段連續(xù)內(nèi)存封裝成一個ByteBuf給應(yīng)用程序使用
Unpooled:每次分配內(nèi)存的時候,直接調(diào)用系統(tǒng)api,向操作系統(tǒng)申請一塊內(nèi)存
- Unsafe和非Unsafe
jdk中有Unsafe對象可以直接拿到對象的內(nèi)存地址,并且基于這個內(nèi)存地址進(jìn)行讀寫操作。那么對應(yīng)的分類的區(qū)別就是是否可以拿到j(luò)dk底層的Unsafe進(jìn)行讀寫操作了。
- 跟進(jìn)
PooledUnsafeHeapByteBuf
@Override
protected byte _getByte(int index) {
//memory:內(nèi)存首地址
//idx(index):偏移量
return UnsafeByteBufUtil.getByte(memory, idx(index));
}
static byte getByte(byte[] array, int index) {
return PlatformDependent.getByte(array, index);
}
public static byte getByte(byte[] data, int index) {
return PlatformDependent0.getByte(data, index);
}
static byte getByte(byte[] data, int index) {
//通過UNSAFE去獲取
return UNSAFE.getByte(data, BYTE_ARRAY_BASE_OFFSET + index);
}
- 跟進(jìn)
PooledHeapByteBuf
@Override
protected byte _getByte(int index) {
return HeapByteBufUtil.getByte(memory, idx(index));
}
static byte getByte(byte[] memory, int index) {
//直接拿到一個數(shù)組
return memory[index];
}
- Head和Direct
Head:是調(diào)用jvm的堆內(nèi)存進(jìn)行分配的,需要被gc進(jìn)行管理
Direct:是調(diào)用jdk的api進(jìn)行內(nèi)存分配,不受jvm控制,不會參與到gc的過程
1.跟進(jìn)UnpooledHeapByteBuf,Head其實就是維持了一個代表內(nèi)存的數(shù)組
private final ByteBufAllocator alloc;
//依賴byte數(shù)組,所有內(nèi)存相關(guān)的操作都在這.
//持有內(nèi)存數(shù)組(堆)
byte[] array;
private ByteBuffer tmpNioBuf;
@Override
protected byte _getByte(int index) {
//直接傳入內(nèi)存數(shù)組
return HeapByteBufUtil.getByte(array, index);
}
static byte getByte(byte[] memory, int index) {
//直接拿到一個數(shù)組
return memory[index];
}
- 跟進(jìn)
UnpooledDirectByteBuf,Direct其實就是調(diào)用jdk的直接內(nèi)存對象DirectByteBuffer
import java.nio.ByteBuffer;
private final ByteBufAllocator alloc;
//依賴jdk底層的ByteBuffer
private ByteBuffer buffer;
private ByteBuffer tmpNioBuf;
private int capacity;
private boolean doNotFree;
@Override
public byte getByte(int index) {
ensureAccessible();
return _getByte(index);
}
@Override
protected byte _getByte(int index) {
//使用buffer
return buffer.get(index);
}
