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
         if(Math.random() > 0.9) System.gc();//line5

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

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.