• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Please Explain Q.10 And Q.11 of Chapter 3 from K&B SCJP6

 
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q10.
Given:

Which two are true about the objects created within main(), and eligible for garbage collection
when line 14 is reached?

ANS : Five objects were created; Two objects are eligible for GC
HOW?

Q.11
Given:

When line 16 is reached, how many objects will be eligible for garbage collection?

ANS : 1
HOW?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q.10. 4 objects are created ; references are dz, da, da[0], d
da[1] and d are pointing to the same object only , so when you assign null to the references , onlyy one object is eligible for GC.

Q.11. no object is eligible for GC
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Q10, five objects are created, let's count!
One in the 8th line, array object.
Two in the 9th line, the Dozens object,
Three - as the instance object of the Dozens object which you've created in the 9th line,
Four - in the 10th line,
Five - as the instance object of the Dozens object, which you've created in the 10th line.

Now, the object created in the 10th line has two reference variable, d and da[1]. But you make them null in the 12th and 13th lines, so that object and its instance variable object dz are eligible for GC!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Q11,
When you make a1 as null in 15th line, the object you've created in line 11 is eligible for GC, and since the static object b1 of that Alpha class, is not eligible for GC. Because the object has a reference b1, which you've created in the 10th line, and the instance variable b2 of the Alpha class is already null, so no object is eligible for GC. So almost, 1 object is eligible for GC!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Q 11, see the attached image,
black lines show the links to objects as they were created.
Red crosses show the nulled references
Magenta lines show the access path of objects.
In this process, only the object a1 is lost, all other objects are available
Q-11.JPG
[Thumbnail for Q-11.JPG]
Question 11
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am totally wrong in my previous post.

Abimaran given the fix!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:I am totally wrong in my previous post.

Abimaran given the fix!



It's just a careless mistake, happened to everyone!
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All,
Sorry, but Q.11 is not cleared yet.....
please describe it more.....
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Kashyap wrote:Q.11 is not cleared yet.....



well, I will try to explain to you


and

though Alphas static/class variable points to the Beta Object(b1) , the variable is not in the instance/object memory a1. class variable store in static storage area of
Class object. so when you say al = null; the object referenced by a1 is eligible to GC. *but Beta b1 = new Beta();* this object is refernced by Alpha's class variable[b1], so even though you assign null to b1 reference in main[b1=null], the Object(Beta b1 = new Beta()) is not eligible to GC.


is that make sense?

 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Seetharaman,

if any reference variable is pointing to NULL then is it eligible for GC?
if yes,
then there are three reference variable here pointing to NULL; what will happen?
otherwise
will you please clear me that when any reference variable would be eligible for GC.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Kashyap wrote:Dear Seetharaman,

if any reference variable is pointing to NULL then is it eligible for GC?
if yes,
then there are three reference variable here pointing to NULL; what will happen?
otherwise
will you please clear me that when any reference variable would be eligible for GC.



You are wrong here! Garbage collection is for objects, not for reference variables or anything! If a variable not referring to any objects, then no point of talking aboult GC on that variable!
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abimaran Sir,
Now, this misunderstanding has been cleared.
Thanks once again.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are Welcome!
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, For Qn. 10. I hope this diagram explains the scenario.
Ans11_justification.JPG
[Thumbnail for Ans11_justification.JPG]
Ans11_Diagram
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic