• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Garbage Collection question

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
One is in the 7th line and the other is in the 10th line!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kamila
i am trying to draw this diagram have look
Filename: gb.bmp
File size: 964 Kbytes
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent diagram,it helps thanks.
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are welcome
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sumit can you please tell why g.gb1 set to null ?
 
Sheriff
Posts: 9690
42
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
This question has been asked before like here and here. You might find the explanations there helpful as well...
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic