• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Questions on GC from Khalid A.Mughal, Chap 9

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 402 & 403 of this book, there are no answers for four GC questions.

1)How many objects are eligible for GC when control reaches (1)?


I think, 9 objects are eligible for GC. is that right?
but the answers are,
(A)0
(B)5
(c)10

can anyone tell the right answer with explanation?
--------------------------------------------------------------------------------------------------------------------------------------
2)How many objects are eligible for GC when control reaches (1)?

I think, 0 objects are eligible for GC. is that correct?

--------------------------------------------------------------------------------------------------------------------------------------

3)How many objects are eligible for GC when control reaches (1)?


I think, 0 objects are eligible for GC here. Is that right?
--------------------------------------------------------------------------------------------------------------------------------------

4) How many objects are reachable when control reaches (1)?


I think 5 objects are reachable.(Integer[] obj with reference array & 4 objects pointed to by the references array[0], array[1], array[2], array[3])
is it correct?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT:
1) 10 objects eligible for gc, variable "obj" is block scoped.
2) 0 objects eligible for gc.
3) 0 objects.
4) 0 object eligible for gc, wrappers are immutable.

i think these are the answers.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) There are 10 objects eligible for GC. obj is declared inside the loop, so its scope is inside the loop, so after the loop is completed, it goes out of scope, thus the object referenced by it is also eligible for GC.
2) I also think 0 objects are eligible for GC.
3) Again I also think 0 objects are eligible for GC. There is actually only one object here which is the int array, and it doesn't goes out of scope or is not set to null.
4) I think 0 objects are eligible for GC. Read this for more info...

[Beaten by just 1 second ]
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey ankit i think in last one, 1 object should be eligible for gc. is it right?
EDIT:
oh got it.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rushikesh sawant wrote:hey ankit i think in last one, 1 object should be eligible for gc. is it right?


Try this modified code
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yea.
um, what about second one? why 0? i think 3.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rushikesh sawant wrote:um, what about second one? why 0? i think 3.


Again try this modified code
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarification.
 
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
Interesting......
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

thanks for the clarification!

but in question 4, the real question is how many objects are reachable? and NOT "HOW MANY OBJECTS ARE ELIGIBLE FOR GC?"
"I think 5 objects are reachable.(Integer[] obj with reference array & 4 objects pointed to by the references array[0], array[1], array[2], array[3]) " --> THAT MEANS NO OBJECTS ARE ELIGIBLE FOR GC, but 5 objects are reachable!

correct me if iam wrong!
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yea, 5 objects are reachable.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AFAIK, the real exam won't ask you how many objects are reachable. Knowing how many objects are reachable automatically tells us how many objects are eligible for GC, so the question is fair, but in the real exam you don't have to worry about whether question is asking for objects reachable or eligible...
reply
    Bookmark Topic Watch Topic
  • New Topic