protected final void deallocate() {
if (handle >= 0) {
final long handle = this.handle;
this.handle = -1;
memory = null;
// 內(nèi)存釋放
chunk.arena.free(chunk, handle, maxLength, cache);
recycle();
}
}
PoolArena
void free(PoolChunk<T> chunk, long handle, int normCapacity, PoolThreadCache cache) {
// 如果是非池化的chunk
if (chunk.unpooled) {
int size = chunk.chunkSize();
destroyChunk(chunk);
activeBytesHuge.add(-size);
deallocationsHuge.increment();
} else {
SizeClass sizeClass = sizeClass(normCapacity);
// 如果這個(gè)chunk能添加到本地cache中加袋,再好不過(guò)
if (cache != null && cache.add(this, chunk, handle, normCapacity, sizeClass)) {
// cached so not free it.
return;
}
freeChunk(chunk, handle, sizeClass);
}
}
void freeChunk(PoolChunk<T> chunk, long handle, SizeClass sizeClass) {
final boolean destroyChunk;
synchronized (this) {
switch (sizeClass) {
case Normal:
++deallocationsNormal;
break;
case Small:
++deallocationsSmall;
break;
case Tiny:
++deallocationsTiny;
break;
default:
throw new Error();
}
// 找到該chunk所屬的PoolChunkList惊楼,做free動(dòng)作
destroyChunk = !chunk.parent.free(chunk, handle);
}
if (destroyChunk) {
// destroyChunk not need to be called while holding the synchronized lock.
destroyChunk(chunk);
}
}
PoolThreadCache
boolean add(PoolArena<?> area, PoolChunk chunk, long handle, int normCapacity, SizeClass sizeClass) {
//去cache里面找滿足條件的MemoryRegionCache
MemoryRegionCache<?> cache = cache(area, normCapacity, sizeClass);
if (cache == null) {
return false;
}
// 將這段內(nèi)存空間添加到該cache的隊(duì)列中
return cache.add(chunk, handle);
}
public final boolean add(PoolChunk<T> chunk, long handle) {
// 將這段空間包裝成Entry
Entry<T> entry = newEntry(chunk, handle);
// 壓入隊(duì)列中
boolean queued = queue.offer(entry);
if (!queued) {
// If it was not possible to cache the chunk, immediately recycle the entry
// 如果添加隊(duì)列失敗陡蝇,立即recycle翰灾,也就是對(duì)象池回收掉
entry.recycle();
}
return queued;
}
PoolChunkList
boolean free(PoolChunk<T> chunk, long handle) {厂画、
// 先調(diào)用PoolChunk的free動(dòng)作
chunk.free(handle);
// 如果這個(gè)chunk的使用率不達(dá)標(biāo)凸丸,那么需要降級(jí)
if (chunk.usage() < minUsage) {
// 從當(dāng)前的倉(cāng)位移除
remove(chunk);
// 移動(dòng)一個(gè)倉(cāng)位存放
return move0(chunk);
}
return true;
}
PoolChunk
void free(long handle) {
// 拿到handle包含的memoryMapIdx,也就是滿二叉樹(shù)的節(jié)點(diǎn)id
int memoryMapIdx = memoryMapIdx(handle);
// 拿到handle包含的subpage的index
int bitmapIdx = bitmapIdx(handle);
// 如果不等于0袱院,說(shuō)明空間已經(jīng)細(xì)分到了subpage
if (bitmapIdx != 0) { // free a subpage
// 拿到對(duì)應(yīng)的PoolSubpage屎慢,用來(lái)替Page管理subPage的對(duì)象
PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)];
assert subpage != null && subpage.doNotDestroy;
// 拿到elemsize的head,來(lái)同步對(duì)subpage進(jìn)行free
PoolSubpage<T> head = arena.findSubpagePoolHead(subpage.elemSize);
synchronized (head) {
if (subpage.free(head, bitmapIdx & 0x3FFFFFFF)) {
return;
}
}
}
// 將釋放的空間大小體現(xiàn)在freeBytes中忽洛,供需要的人知曉
freeBytes += runLength(memoryMapIdx);
// 將id節(jié)點(diǎn)的value重置成原生的depth腻惠,make space reuse
setValue(memoryMapIdx, depth(memoryMapIdx));
// 更新從id開(kāi)始到root沿途的value
updateParentsFree(memoryMapIdx);
}
updateParentsFree
private void updateParentsFree(int id) {
int logChild = depth(id) + 1;
while (id > 1) {
int parentId = id >>> 1;
byte val1 = value(id);
byte val2 = value(id ^ 1);
logChild -= 1;
// 如果左右子節(jié)點(diǎn)都等于logChild,那么父節(jié)點(diǎn)一定是logChild-1
if (val1 == logChild && val2 == logChild) {
setValue(parentId, (byte) (logChild - 1));
} else {
// 如果左右節(jié)點(diǎn)不相等欲虚,那么父節(jié)點(diǎn)取最小的那個(gè)
byte val = val1 < val2 ? val1 : val2;
setValue(parentId, val);
}
id = parentId;
}
}
PoolSubpage
boolean free(PoolSubpage<T> head, int bitmapIdx) {
if (elemSize == 0) {
return true;
}
// 將bitmap里面之前占用的bit位清0集灌,表示邏輯上釋放空間
int q = bitmapIdx >>> 6;
int r = bitmapIdx & 63;
assert (bitmap[q] >>> r & 1) != 0;
bitmap[q] ^= 1L << r;
// 將該位置設(shè)置下個(gè)可用的位置
setNextAvail(bitmapIdx);
// 如果之前還沒(méi)有位置, 經(jīng)過(guò)上面的處理, 想必已經(jīng)騰出了位置
// 那么加入到head后面, 等待對(duì)外提供subpage
if (numAvail ++ == 0) {
addToPool(head);
return true;
}
if (numAvail != maxNumElems) {
return true;
} else {
// 如果整個(gè)page都被釋放
if (prev == next) {
return true;
}
doNotDestroy = false;
removeFromPool();
return false;
}
}
removeFromPool
// 整個(gè)實(shí)現(xiàn)就是自旋,將head解綁复哆。
// 我們知道head好比是arena初始的一個(gè)index欣喧,用這個(gè)head能定位所有相同elemsize的內(nèi)存空間
// 那么這個(gè)PoolSubpage最終會(huì)被GC掉
private void removeFromPool() {
assert prev != null && next != null;
prev.next = next;
next.prev = prev;
next = null;
prev = null;
}