-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
type: questionFurther information is requestedFurther information is requested
Description
Question
Since #3245 get merged, I think we can add a MemorySafeLinkedBlockingQueue
which use MemoryLimitCalculator#maxAvailable
:
/**
* Can completely solve the OOM problem caused by {@link java.util.concurrent.LinkedBlockingQueue},
* does not depend on {@link java.lang.instrument.Instrumentation} and is easier to use than
* {@link org.apache.shenyu.common.concurrent.MemoryLimitedLinkedBlockingQueue}.
*/
public class MemorySafeLinkedBlockingQueue<E> extends LinkedBlockingQueue<E> {
private static final long serialVersionUID = 8032578371749960142L;
private int maxFreeMemory;
public MemorySafeLinkedBlockingQueue(final int maxFreeMemory) {
super(Integer.MAX_VALUE);
this.maxFreeMemory = maxFreeMemory;
}
public MemorySafeLinkedBlockingQueue(final Collection<? extends E> c,
int maxFreeMemory) {
super(c);
this.maxFreeMemory = maxFreeMemory;
}
/**
* set the max free memory.
*
* @param maxFreeMemory the max free memory
*/
public void setMaxFreeMemory(final int maxFreeMemory) {
this.maxFreeMemory = maxFreeMemory;
}
/**
* get the max free memory.
*
* @return the max free memory limit
*/
public int getMaxFreeMemory() {
return maxFreeMemory;
}
/**
* determine if there is any remaining free memory.
*
* @return true if has free memory
*/
public boolean hasRemainedMemory() {
return MemoryLimitCalculator.maxAvailable() > maxFreeMemory;
}
@Override
public void put(final E e) throws InterruptedException {
if (hasRemainedMemory()) {
super.put(e);
}
}
@Override
public boolean offer(final E e, final long timeout, final TimeUnit unit) throws InterruptedException {
return hasRemainedMemory() && super.offer(e, timeout, unit);
}
@Override
public boolean offer(final E e) {
return hasRemainedMemory() && super.offer(e);
}
}
Metadata
Metadata
Assignees
Labels
type: questionFurther information is requestedFurther information is requested