protected final void deallocate() {
if (handle >= 0) {
final long handle = this.handle;
this.handle = -1;
memory = null;
// 内存释放
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);
// 如果这个chunk能添加到本地cache中,再好不过
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动作
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;
}
// 将这段内存空间添加到该cache的队列中
return cache.add(chunk, handle);
}
public final boolean add(PoolChunk<T> chunk, long handle) {
// 将这段空间包装成Entry
Entry<T> entry = newEntry(chunk, handle);
// 压入队列中
boolean queued = queue.offer(entry);
if (!queued) {
// If it was not possible to cache the chunk, immediately recycle the entry
// 如果添加队列失败,立即recycle,也就是对象池回收掉
entry.recycle();
}
return queued;
}
PoolChunkList
boolean free(PoolChunk<T> chunk, long handle) {、
// 先调用PoolChunk的free动作
chunk.free(handle);
// 如果这个chunk的使用率不达标,那么需要降级
if (chunk.usage() < minUsage) {
// 从当前的仓位移除
remove(chunk);
// 移动一个仓位存放
return move0(chunk);
}
return true;
}
PoolChunk
void free(long handle) {
// 拿到handle包含的memoryMapIdx,也就是满二叉树的节点id
int memoryMapIdx = memoryMapIdx(handle);
// 拿到handle包含的subpage的index
int bitmapIdx = bitmapIdx(handle);
// 如果不等于0,说明空间已经细分到了subpage
if (bitmapIdx != 0) { // free a subpage
// 拿到对应的PoolSubpage,用来替Page管理subPage的对象
PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)];
assert subpage != null && subpage.doNotDestroy;
// 拿到elemsize的head,来同步对subpage进行free
PoolSubpage<T> head = arena.findSubpagePoolHead(subpage.elemSize);
synchronized (head) {
if (subpage.free(head, bitmapIdx & 0x3FFFFFFF)) {
return;
}
}
}
// 将释放的空间大小体现在freeBytes中,供需要的人知晓
freeBytes += runLength(memoryMapIdx);
// 将id节点的value重置成原生的depth,make space reuse
setValue(memoryMapIdx, depth(memoryMapIdx));
// 更新从id开始到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;
// 如果左右子节点都等于logChild,那么父节点一定是logChild-1
if (val1 == logChild && val2 == logChild) {
setValue(parentId, (byte) (logChild - 1));
} else {
// 如果左右节点不相等,那么父节点取最小的那个
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;
// 将该位置设置下个可用的位置
setNextAvail(bitmapIdx);
// 如果之前还没有位置, 经过上面的处理, 想必已经腾出了位置
// 那么加入到head后面, 等待对外提供subpage
if (numAvail ++ == 0) {
addToPool(head);
return true;
}
if (numAvail != maxNumElems) {
return true;
} else {
// 如果整个page都被释放
if (prev == next) {
return true;
}
doNotDestroy = false;
removeFromPool();
return false;
}
}
removeFromPool
// 整个实现就是自旋,将head解绑。
// 我们知道head好比是arena初始的一个index,用这个head能定位所有相同elemsize的内存空间
// 那么这个PoolSubpage最终会被GC掉
private void removeFromPool() {
assert prev != null && next != null;
prev.next = next;
next.prev = prev;
next = null;
prev = null;
}
网友评论