File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic

Garbage collection

vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
From Whizlab SCJP 1.4

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.

SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Lakuma Yalamanchili

Joined: Sep 24, 2008
Posts: 26

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!

[ September 30, 2008: Message edited by: Lakuma Yalamanchili ]
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
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".
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Yes, Option 3 and 4 are right.

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 ]

Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 288
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.
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Well, looking at the options, the first 2 can be easily eliminated. So,its the remaining two.
mukki pandey
Ranch Hand

Joined: Sep 22, 2008
Posts: 58
option 3 is so confusing

i think 3 & 4 are right
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Thanks everybody.
[ October 01, 2008: Message edited by: vidhya suvarna ]
I agree. Here's the link:
subject: Garbage collection
It's not a secret anymore!