• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exam lab question 42

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How many objects are eligible for garbage collection ?

i think answer should be 1

bc--> new Gabc which contains 2 references of type Gabc
hence,
bc.ob[0]---->new Gabc //line1
bc.ob[1]---->new gabc(let this be D)

D contains 2 refernces of type Gabc
hence,
d.ob[0]---->new Gabc
d.ob[1]---->bc.ob[0] //line2

so,when bc.ob[0]=null;

then line 1 would be equivalent to
null----> new Gabc

then line 2 would be equivalent to
d.ob[1]---->null

so,object at line 1 would be eligible for GC



 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer should be zero.

When the following code is run:

the reference to newly created object is changed to null.

But the object is not eligible for Garbage Collection.
It can be reached through bc.ob[1].ob[1] as that action is performed in previous line:
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trivikram :
the object is not eligible for Garbage Collection.
It can be reached through bc.ob[1].ob[1] as that action is performed in previous line:

bc.ob[0]---->new Gabc()

and
bc.ob[1].ob[1]----> bc.ob[0]


so,after bc.ob[0]---> null


bc.ob[1].ob[1]---->null


so,object at line 1 would be eligible for GC


e.g
if
a---> b--->c
and
b--->null
then a ----> null too
as refers to b
(here a, b are references and c is object)
so c would be GC candidate ..
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:
e.g
if
a---> b--->c
and
b--->null
then a ----> null too
as refers to b
(here a, b are references and c is object)


It's incorrect.
When you say a---> b--->c
It's a ---> c
and also b ---> c

when b = null, it just affects b ---> c, it doesn't affect the reference a
So, a ---> c still holds true
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you still haven't understood, try running the following code and go through the output:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic