• Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions on GC from Khalid A.Mughal, Chap 9  RSS feed

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • 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?
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
yea.
um, what about second one? why 0? i think 3.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for clarification.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting......
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
yea, 5 objects are reachable.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!