[#5833] Ensure direct memory is released when DirectPoolArena is collected
Motivation: We need to ensure we release all direct memory once the DirectPoolArena is collected. Otherwise we may never reclaim the memory and so leak memory. Modifications: Ensure we destroy all PoolChunk memory when DirectPoolArena is collected. Result: Free up unreleased memory when DirectPoolArena is collected.
This commit is contained in:
parent
bb81496076
commit
b861961e60
@ -619,6 +619,29 @@ abstract class PoolArena<T> implements PoolArenaMetric {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final void finalize() throws Throwable {
|
||||
try {
|
||||
super.finalize();
|
||||
} finally {
|
||||
destroyPoolSubPages(smallSubpagePools);
|
||||
destroyPoolSubPages(tinySubpagePools);
|
||||
destroyPoolChunkLists(qInit, q000, q025, q050, q075, q100);
|
||||
}
|
||||
}
|
||||
|
||||
private static void destroyPoolSubPages(PoolSubpage<?>[] pages) {
|
||||
for (PoolSubpage<?> page : pages) {
|
||||
page.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private void destroyPoolChunkLists(PoolChunkList<T>... chunkLists) {
|
||||
for (PoolChunkList<T> chunkList: chunkLists) {
|
||||
chunkList.destroy(this);
|
||||
}
|
||||
}
|
||||
|
||||
static final class HeapArena extends PoolArena<byte[]> {
|
||||
|
||||
HeapArena(PooledByteBufAllocator parent, int pageSize, int maxOrder, int pageShifts, int chunkSize) {
|
||||
|
@ -461,4 +461,8 @@ final class PoolChunk<T> implements PoolChunkMetric {
|
||||
.append(')')
|
||||
.toString();
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
arena.destroyChunk(this);
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,6 @@ final class PoolChunkList<T> implements PoolChunkListMetric {
|
||||
private final int minUsage;
|
||||
private final int maxUsage;
|
||||
private final int maxCapacity;
|
||||
|
||||
private PoolChunk<T> head;
|
||||
|
||||
// This is only update once when create the linked like list of PoolChunkList in PoolArena constructor.
|
||||
@ -223,4 +222,13 @@ final class PoolChunkList<T> implements PoolChunkListMetric {
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
void destroy(PoolArena<T> arena) {
|
||||
PoolChunk<T> chunk = head;
|
||||
while (chunk != null) {
|
||||
arena.destroyChunk(chunk);
|
||||
chunk = chunk.next;
|
||||
}
|
||||
head = null;
|
||||
}
|
||||
}
|
||||
|
@ -227,4 +227,10 @@ final class PoolSubpage<T> implements PoolSubpageMetric {
|
||||
public int pageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
if (chunk != null) {
|
||||
chunk.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user