• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection question

 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following question ask how many object are eligible for GC after line 10.



The answer is 3 but I don't know which one is the third object. To clarify it I was trying to drew the picture what is happening(but I am not sure is it correct)

line: 7. Garb gb1=new Garb(new Garb(new Garb(null))); // 3 objects created:
gb1-->|_|
gb1.g-->|_|
gb1.g.g->|_|

line: 8. gb1.g.g=new Garb(new Garb(null)); // 2 objects created, 1 eligible to GC:
gb1-->|_|
gb1.g-->|_|
gb1.g.g->|_| |_| //1st object eligible for GC
gab1.g.g.g->|_|

line: 9. Garb gb2=new Garb(new Garb(new Garb(gb1))); // Another 3 objects created:
gb1-->|_|<--gb2.g.g.g
gb1.g-->|_|
gb1.g.g->|_| |_|//1st object eligible to GC
gab1.g.g.g->|_|
gb2-->|_|
gb2.g-->|_|
gb2.g.g-->|_|

line: 10. gb2.g=gb1.g;// 2nd object eligile to GC
gb1-->|_|<--gb2.g.g.g
gb1.g-->|_|<--gb2
gb1.g.g->|_| |_|//1st object eligible to GC
gab1.g.g.g->|_|
|_|//2nd
gb2.g-->|_|
gb2.g.g-->|_|

Thus, I am not sure which is the third object available for GC.

Any help will be appreciated.

Kamila

 
Kacper Jonczyk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kamila,

You're almost there. At line 10 there are 2 additional objects eligible for GC instead of 1.

Gb2.g refers to the object which has a reference to another object. After changing gb2.g reference, both of them will be eligible for GC, because neither of them can be accessed any longer.

Hope this helps. Please correct me if I'm wrong. It's bit twisted example although
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One is in the 7th line and the other is in the 10th line!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Could you please QUOTEYOURSORCES from where you get this question.

 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kacper, Abimaran thanks for the answer
Kacper I think you are correct.
Shanky, the question is from Deveka ExamLab simulator.

Regards,
Kamila
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

This question will definitely toughen you up for the real exam, but to be clear, it's a little bit "over the top". Questions on the real exam won't be this twisty-turny.
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kamila
i am trying to draw this diagram have look
Filename: gb.bmp
Description:
File size: 942 Kbytes
[Download gb.bmp] Download Attachment
 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent diagram,it helps thanks.
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are welcome
 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumit can you please tell why g.gb1 set to null ?
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has been asked before like here and here. You might find the explanations there helpful as well...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic