Ensure WeakOrderQueue can be collected fast enough

Motivation:

Commit afafadd3d7 introduced a change which stored the Stack in the WeakOrderQueue as field. This unfortunally had the effect that it was not removed from the WeakHashMap anymore as the Stack also is used as key.

Modifications:

Do not store a reference to the Stack in WeakOrderQueue.

Result:

WeakOrderQueue can be collected correctly again.
This commit is contained in:
Norman Maurer 2016-07-22 20:41:07 +02:00
parent 3ebbd96820
commit 94d7557dea

View File

@ -223,7 +223,7 @@ public abstract class Recycler<T> {
private WeakOrderQueue next;
private final WeakReference<Thread> owner;
private final int id = ID_GENERATOR.getAndIncrement();
private final Stack<?> stack;
private final AtomicInteger availableSharedCapacity;
WeakOrderQueue(Stack<?> stack, Thread thread) {
head = tail = new Link();
@ -232,19 +232,42 @@ public abstract class Recycler<T> {
next = stack.head;
stack.head = this;
}
this.stack = stack;
// Its important that we not store the Stack itself in the WeakOrderQueue as the Stack also is used in
// the WeakHashMap as key. So just store the enclosed AtomicInteger which should allow to have the
// Stack itself GCed.
availableSharedCapacity = stack.availableSharedCapacity;
// We allocated a Link so reserve the space
boolean reserved = stack.reserveSpace(LINK_CAPACITY);
boolean reserved = reserveSpace(LINK_CAPACITY);
assert reserved;
}
private boolean reserveSpace(int space) {
assert space >= 0;
for (;;) {
int available = availableSharedCapacity.get();
if (available < space) {
return false;
}
if (availableSharedCapacity.compareAndSet(available, available - space)) {
return true;
}
}
}
private void reclaimSpace(int space) {
assert space >= 0;
availableSharedCapacity.addAndGet(space);
}
void add(DefaultHandle<?> handle) {
handle.lastRecycledId = id;
Link tail = this.tail;
int writeIndex;
if ((writeIndex = tail.get()) == LINK_CAPACITY) {
if (!stack.reserveSpace(LINK_CAPACITY)) {
if (!reserveSpace(LINK_CAPACITY)) {
// Drop it.
return;
}
@ -314,7 +337,7 @@ public abstract class Recycler<T> {
if (srcEnd == LINK_CAPACITY && head.next != null) {
// Add capacity back as the Link is GCed.
stack.reclaimSpace(LINK_CAPACITY);
reclaimSpace(LINK_CAPACITY);
this.head = head.next;
}
@ -339,7 +362,7 @@ public abstract class Recycler<T> {
private DefaultHandle<?>[] elements;
private final int maxCapacity;
private int size;
private final AtomicInteger availableSharedCapacity;
final AtomicInteger availableSharedCapacity;
private volatile WeakOrderQueue head;
private WeakOrderQueue cursor, prev;
@ -352,24 +375,6 @@ public abstract class Recycler<T> {
elements = new DefaultHandle[min(INITIAL_CAPACITY, maxCapacity)];
}
boolean reserveSpace(int space) {
assert space >= 0;
for (;;) {
int available = availableSharedCapacity.get();
if (available < space) {
return false;
}
if (availableSharedCapacity.compareAndSet(available, available - space)) {
return true;
}
}
}
void reclaimSpace(int space) {
assert space >= 0;
availableSharedCapacity.addAndGet(space);
}
int increaseCapacity(int expectedCapacity) {
int newCapacity = elements.length;
int maxCapacity = this.maxCapacity;