java.lang.OutOfMemoryError:超出GC开销限制-LinkedBlockingQueue
我已经实现了我自己的线程池执行器,如下所示java.lang.OutOfMemoryError:超出GC开销限制-LinkedBlockingQueue,java,threadpoolexecutor,blockingqueue,Java,Threadpoolexecutor,Blockingqueue,我已经实现了我自己的线程池执行器,如下所示 private final Queue<?> inProgressQueue; private final Queue<?> pendingQueue; public ThreadPoolExecutorImpl(int corePoolSize, int maximumQueueSize, long keepAliveTime, TimeUnit unit) { super(PoolSize,
private final Queue<?> inProgressQueue;
private final Queue<?> pendingQueue;
public ThreadPoolExecutorImpl(int corePoolSize, int maximumQueueSize, long keepAliveTime, TimeUnit unit) {
super(PoolSize, PoolSize, keepAliveTime, unit, new LinkedBlockingQueue<Runnable>());
this.maximumQueueSize = maximumQueueSize;
this.inProgressQueue = new ConcurrentLinkedQueue<>();
this.pendingQueue = new ConcurrentLinkedQueue<>();
}
使用上述代码可以正常工作2到3天,没有任何问题。但4天后,除以下情况外,它已停止运行
java.lang.OutOfMemoryError: GC overhead limit exceeded at
java.util.concurrent.LinkedBlockingQueue.offer(LinkedBlockingQueue.java:415)
java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1364)..
有谁能帮我解释一下,为什么会发生这种行为?以及如何解决这个问题。还是改用Runnable?
线程。等等
?这实际上不应该编译,除非您有一个名为Thread
的变量,这将非常糟糕。这可能是因为您的队列占用了太多的元素。您是如何使用队列元素的?查看调用offer()
方法时出现的错误。您可以为队列设置限制大小,一旦达到限制,就开始删除事件或启动更多使用者来使用队列编辑为睡眠(10000),,,,谢谢海峰,但这里的问题出在offer()中,它无法创建此部分。节点=新节点(e);因为我使用了LinkedBlockingQueue,所以它实际上不是一个固定的大小。您的瓶颈是向队列中添加元素比从队列中消耗元素要快,因此它迟早会消耗内存。您可以创建一个计数器来存储队列大小(concurrentlinkedqueue方法size()为O(N)),一旦队列大小达到限制,就删除新的传入元素。或者,如果不允许删除元素,则可以启动更多使用者,并确保消费比生产快
public class Callableclass extends callable{
@Override
public String call() {
Thread.sleep(10000);
//code
}
}
java.lang.OutOfMemoryError: GC overhead limit exceeded at
java.util.concurrent.LinkedBlockingQueue.offer(LinkedBlockingQueue.java:415)
java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1364)..