Capacity planning
December 4th, 2007 by Stefan Fußenegger | Published in Spring
Today, I discovered a nice little gotcha in the Spring source, namely in org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor, while I was implementing a priority task queue. The mentioned class uses a template method to create the backing queue for pending tasks:
protected BlockingQueue createQueue(int queueCapacity) {...}
I’ve overridden this method like so:
protected BlockingQueue createQueue(int queueCapacity) {
return new PriorityBlockingQueue(queueCapacity, new Comparator() {...});
}
However, this didn’t work as I got:
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
Why? By default, the ThreadPoolTaskExecutor uses
new LinkedBlockingQueue(queueCapacity);
With a default queueCapacity of Integer.MAX_VALUE … This is ok, as the capacity of a LinkedBlockingQueue is its maximum capacity while the capacity of the PriorityBlockingQueue is its initial capacity (and used to create a array of the given size). Unfortunately, it isnt’t even possible to limit the size of PriorityBlockingQueue at all. Therefore, the parameter should be ignored for PriorityBlockingQueues.