Sunday, July 27, 2014

Posted by Anmol Sharma
While garbage collection is automated in java, still it provides system.gc() method which force run the garbage collector and perform clean-up tasks. And at the same time, it is never recommended to run the garbage collector explicitly which causes the system to be inefficient and even the JVM will ignore the explicit method in most cases.

 There are various reasons not to use explicit gc but it is not the matter of discussion. The point of discussion is that many times it is asked in what circumstances or applications System.gc() should be used. Why it all exist then? First of all at beginners level like us we should hardly bother about using System.gc() at all. Let the automatic garbage collection do the job. But for a general idea it is interesting to note that there are some applications and circumstances where explicit garbage collections proves to be quite useful. And that’s why this method is not deprecated from java.

- In applications when user actually wants to know how much memory is lying unused by expired instances, in other words to find about memory leaks called as profiling.

- In android applications while creating bitmaps to prevent out of memory errors.

- Some task managers where user clicks a button and kills the task.

- Before performing benchmarking tests of an application.

- During development, debugging and testing phases of an applications

- When you want to prove yourself as a bad computer science student :P

I just cannot come across any other reason why one should even bother about manual resource collector when we have automatic memory management which is quite efficient.

Just in case you didn’t understand the above text then I am sure you will understand about this with the help of following piece of witticism  I just wrote while thinking all about this ;P

/* Me during a hypothetical (kalpanik) job interview in an IT company... */

Interviewer – Can we perform explicit garbage collection using system.gc() in java? Explain in most simple words taking real life example…?

Me – Yes, we can perform explicit garbage collection using system.gc() in java but mostly it will be totally ignored by JVM. There is no guarantee if JVM will even bother about the presence of the system.gc() method.
Taking a real life example, It’s actually like proposing a girl or asking her out for a cup of coffee but there is no guarantee that she will accept your request or not. It will be totally ignored mostly. So let the garbage collector run on its own  explicit garbage collection matlab ek tarfa pyaar  …err Dil ki baat thodi technical hogai ://

Interviewer with tears in eyes – Bhai Single lagtey ho  …Kitni salary loge?

PS - Do not imitate this in real life interview, you just might be blacklisted forever from that company ;P :--D

In the next article I will try to explain the overall garbage collection process in java and if possible try to compare with method in other languages.

So that’s all folks, thanks for reading.

0 comments:

Post a Comment