2

Ok today I was in an interview and I have been coding Java for years. The interview said "Java garbage collection is a tricky one I had few friends who had struggled figuring out. How are you doing on that?". Was she trying to trick me? or is my entire life a lie and java does not have automatic garbage collection?

Because as far as i know java has automatic garbage collection and you can call System.gc() to collect some resources but this does not force the object to get destroyed. It is still decided by JVM.

Am I wrong?

Thomas
  • 1,123
  • 3
  • 13
  • 36
  • Did you bother Googling this? – tnw May 10 '17 at 18:44
  • yes it says its automatic. but i am getting paranoid about this – Ali Kurabiyeci May 10 '17 at 18:46
  • Manual invoke of GC is still not recommended: [link](http://stackoverflow.com/questions/2414105/why-is-it-bad-practice-to-call-system-gc) – Bogdan Kobylynskyi May 10 '17 at 18:47
  • @BogdanKobylinsky so it was a trick question? – Ali Kurabiyeci May 10 '17 at 18:48
  • refer [this](http://stackoverflow.com/questions/5086800/java-garbage-collection) too. Manual GC is neither preferred nor recommended. – Anant666 May 10 '17 at 18:48
  • specifically, you cannot manually free individual objects. if this were allowed, then you could force the JVM to point to invalid memory. – Rob May 10 '17 at 18:54
  • 4
    If I were you, I would simply had asked to clarify what "How are you doing on that?" means. Maybe the interviewer meant to ask if you were able to diagnose memory leaks, or to choose the best GC algorithm, or to tune the GC. But asking "How are you doing on that?" is frankly a very bad question. – JB Nizet May 10 '17 at 18:56
  • This is an excellent interview question, I need to remember it. – eckes May 11 '17 at 04:14
  • How do you conclude from that question that it's not automatic? – the8472 May 11 '17 at 14:16
  • @eckes please dont make any more students suffer... – Ali Kurabiyeci May 11 '17 at 17:14
  • 1
    Perhaps, the correct answer to “How are you doing on that?” would be “Fine. Thanks.” The really strange thing is not the statement from the interviewer, but that you apparently went away without asking what the hell this is supposed to mean. – Holger Dec 22 '17 at 14:33
  • @Holger hahahaha! Nice – Eugene Dec 22 '17 at 19:28

2 Answers2

2

Just because the garbage collection is automatic doesn't means you can just completely ignore the implications of object allocation and cleanup and how the GC works.

For many applications, especially simple ones, it will be fine to just let the GC do it's thing. Although even then you have to make sure you are not holding onto references longer than needed.

As your application becomes bigger and more complicated, especially if you are using any multi-threading, the impact the GC poses becomes more of a concern and it becomes more important to understand how everything is working in your code and what the GC is doing.

puhlen
  • 8,400
  • 1
  • 16
  • 31
1

GC is automatic, yes.
But some practices related to GC may be subtle and so should be known and understood to avoid memory leak or undesirable behavior.

For example :finalize()

is called by the garbage collector on an object when garbage collection determines that there are no more references to the object

according to the javadoc but in reality it could never be called.

Other example : GC collects remove the objects that are no referenced any longer but you could have a "heavy" object that is not any longer required but still referenced by a referenced object. So the heavy would not be elligible to be collected.
Using monitoring tools as JVisualVM shows sometimes some surprises and I say : Ah, this big object is still referenced here...

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
davidxxx
  • 125,838
  • 23
  • 214
  • 215