954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Garbage Colloection tradeoff

Hello,
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:

public void loop(BlockingQueue<Runnable> queue)
{
    for(Runnable runnable : queue)//line2
    {
         runnable.run();//line4
         if(Math.random() > 0.9) System.gc();//line5
    }
//line7    
}

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 invisible state. 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.

1st Question) Is line 5 needed? Any other substitute
2nd Question) If I have to have line 5, i found out the performance was very very bad when compared to not having it.

ultimately garbage collection has to happen? Now when it should happen, I am unable to figure out

purijatin
Light Poster
41 posts since Jun 2009
Reputation Points: 10
Solved Threads: 5
 

They are not eligible for GC, with or without "line 5", as the "Runnables" are still referenced in the queue.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

very true, actually incomplete code,
assume that i am taking the element out of the queue.
and then running it. Then the previous runnable will be eligible for gc

purijatin
Light Poster
41 posts since Jun 2009
Reputation Points: 10
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: