Which of the following are true about garbage collection? Select 2 options 1)You can directly free the memory allocated by an object. 2)You can directly run the garbage collector whenever you want. 3)The garbage collector informs your object when it is about to be garbage collected. 4)The garbage collector runs in low memory situation.
According to me option 2 for sure cannot be one of the answer. I think option 4 can be one of the answer. Can someone kindly help me know which 2 options are right. Thanks
You are right about option 2 not being right, you can ask the JVM, but that never guarantees running of the GC! Option 1 is anyways ruled out!
Option 3 - As finally runs on the object before it is GCed, if that is what option 3 refers to, it has to be true! Option 4 - True. As the topic in K&B states that the JVM already runs the GC before it gives you the OutOfMemory exception, which implies that GC is run in low memory conditions!
So looks like Option 3 and 4 are right, but option 3 kind of puts me into thinking regarding the underlying meaning that it has..!! Someone add to this please and correct me if I'm wrong!
Hi Lakuma, Yes options 4 is obviously right. And even i say that the GC informs the object when it is about to be garbage collected. To be more clear GC will call the finalize() method of the object before that object is garbage collected. I think this is what option 3 means "informs your object".
Garbage Collector cannot assure you that you will never run out of memory but assures you that whatever memory is availabe, will be managed effectively. [ October 01, 2008: Message edited by: chander shivdasani ]
still there is one case when GC will not inform.Say GC has called the finalize method before garbage collecting, but in finalize method again you provide reference of that object to some other object. then GC will not collect that object .but later on when GC try to collect that object again ,it will not call finalize method again i.e. it call finalize method only once in object life time.