Note that there are some explanatory texts on larger screens.

plurals
  1. POIs my ThreadPoolExecutor leaking memory?
    text
    copied!<p>I'm using a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html" rel="nofollow"><code>ThreadPoolExecutor</code></a> to run tasks. The backend is a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/SynchronousQueue.html" rel="nofollow"><code>SynchronousQueue</code></a>, so if the executor is already perfoming a task, it throws the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/RejectedExecutionException.html" rel="nofollow"><code>RejectedExecutionException</code></a>. Here's a simple test case:</p> <pre><code>public class ExecutorTest { final static Worker worker = new Worker(); public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue&lt;Runnable&gt;()); while (true) { try { executor.execute(worker); }catch (RejectedExecutionException e) { } } } static class Worker implements Runnable { private int i = 0; private long start = System.currentTimeMillis(); @Override public void run() { try { Thread.sleep(1000); System.out.println(++i + " " + (System.currentTimeMillis() - start)); } catch (InterruptedException ex) { } } } } </code></pre> <p>The expected bahavious is: Execute the worker and after sleeping for a second, print out i (representing how often the worker has been executed so far) and the amount of milliseconds since the worker was created. So I'm expecting:</p> <pre><code>1 1015 2 2015 3 3016 4 4017 </code></pre> <p>This works fine for a while, but after almost on hour:</p> <pre><code>2919 2922196 2920 2942951 2921 2990407 </code></pre> <p>So the amount of time between one worker execution and the next one is 20 seconds (2919->2920) and 38 seconds (2920->2921) and so forth. Everything becomes extremely slow and the jvm spends a lot of time in garbage collection. Finally (after a few days) I run into an OutOfMemoryError.</p> <p>I'm running this with -Xmx8M (I assume the effect appears much later with more heap space) on Oracle's JVM 1.7.0_07 on a 64bit Linux machine. I'd appreciate any pointers, but probably I'm just missing the obvious.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload