This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection Question" Watch "Garbage Collection Question" New topic

Garbage Collection Question

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
How many objects are eligible for garbage collection at #1 ?

Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

How many do YOU think there are?
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
I think 2.
1. new StartBugs() and 2. javas
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

the object to which reference variable javas is referring wont be garbage collected because even thought javas is assigned to null, bugs and bugs_INP is referring to it. also at #1 there is this reference to new StartBugs(). so i think zero objects will be garbage collected.

guys correct me if i'm wrong. I myself is little shaky on GC questions so would like to see more posts on this topic. have a great day.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Hi Gurpreet, The answer is 1 [explanation : "javas is the only object that cannot be reached by the JVM. The this reference will still be available at #1 "]
can you explain little more I don't understand what is happening to this reference?
can someone draw the diagram for it?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Janki Shah wrote:can someone draw the diagram for it?

You will benefit a great deal more from a diagram you draw yourself. But you are exactly right to want a diagram; that is the best way, by far, to approach GC questions.
subject: Garbage Collection Question
Similar Threads
Objects eligible for Garbage collection question
Garbage collection with immutable short.
help scjp5.0 question
Garbage Collection
Garbage collection