title: Garbage collection overload, Java
body: <p>The problem is that some how due to garbage collection timings I am having tradeoff's in my performance. The issue can be generalized as:</p>
  
  <pre><code>  public void loop(BlockingQueue&lt;Runnable&gt; queue)
    {
    int j = queue.size();
    for(int i =0; i&lt;j;i++)//line2
    {
    Runnable runnable = queue.take();
    runnable.run();//line4
  
    if(Math.random() &gt; 0.9) System.gc();//line5
  
    }
  
    //line7 //will 'runnable = null;' answer the question, logically it looks right
  
    }
  </code></pre>
  
  <p>Now normally the queue passed as argument will contain more than 40,000 elements normally.
  And because I am iterating over the queue in a loop, even though the already 'run' objects are out of scope, they are still not available for garbage Collection because they are in <a href="http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html#997414" rel="nofollow">invisible state</a>. Hence if I do not have the line 5, then suddenly there will be a huge load on the garbage collector when the method goes out of the stack. Imagine if concurrently many thread accessing the menthod.</p>
  
  <p>My questions:</p>
  
  <ol>
  <li>Is line 5 needed? Is there any other substitute?</li>
  <li>If I have to have line 5, I found out the performance was very very bad when compared to not having it.</li>
  </ol>
  
  <p>Ultimately garbage collection has to happen? I am unable to figure out when it should happen. </p>
  
  <p>PS: Javascript is disabled on my computer hence can't comment for answers. I shall edit the post here for the comments:</p>
  
  <p>@amit: I have changed the code, I think you have understood the essence of the problem. The code is just a sample.</p>
  
  <p>@Tobi: Thanks, but how will setting up a bigger heap size, solve the problem. Thats only delaying the time of gc.
  So you think it will perform the best without manual gc?
  Further from <a href="http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html" rel="nofollow">http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html</a>, it says that only once the method is taken of the stack, only then in this case it would be available for garbage collection. And i tried with finalize() (by having a print, not the right way, but should atleast work once for 100000 objects), there is absolutely no gc.</p>
  
  <p>@Paolo: Thanks. What i am trying to implement is a pipelining model, where every thread has a meassage queue, basically a framework where any thread can post a runnable to the other thread (if it has some work with the thread), and the other thread will execute them after soem time(when it is idle)
  Ans when i meant, overload when the method comes out of the stack, what i mean is that garbage colelction will eventually happen, if it happens later, then clearing 40,000 elements will take a lot time</p>
  
  <p>@ Joachim Sauer : System.gc can collect invisible objects, It is just that garbage collector doesn't collect them automatically. But when forced, it does as per: <a href="http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html" rel="nofollow">http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html</a></p>