This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

SC Exam Lab 3 (Question 16), GC

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,, i being drawing few times of the statement below. But still can't get it.

 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, i left out.

Question is,
How many objects are eligible for GC, after executing the line-10 of this program?

The answer is 3 objects will be GCed.

Thanks

Regards,
Edmen
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Hope this modified code helps you in identifying the objects garbage collected. I did not have to draw the diagram to identify the objects eligible for garbage collection.



Output:

finalize called for: Garb_6
finalize called for: Garb_7
finalize called for: Garb_1


[ November 29, 2008: Message edited by: Harvinder Thakur ]
[ November 29, 2008: Message edited by: Harvinder Thakur ]
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain this with diagram...?
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Let make it clear.

First,
Garb gb1 = new Garb(new Garb(new Garb(null, "Garb_1"),"Garb_2"), "Garb_3");
will be:
gb1 -> Garb_3 -> Garb_2 -> Garb_1

Second,
gb1.g.g = new Garb(new Garb(null, "Garb_4"),"Garb_5")
will be:
Garb_2 -> Garb_5 -> Garb_4

Garb_1 is GCed here!!

Third,
Garb gb2 = new Garb(new Garb(new Garb(gb1,"Garb_6"), "Garb_7"), "Garb_8");
will be:
gb2 -> Garb_8 -> Garb_7 -> Garb_6 -> Garb_3 (pointing to reference of gb1)

Forth,
gb2.g = gb1.g;
will be:
Garb_8 -> Garb_2 (pointing to reference of gb1.g)

Garb_7 and Garb_6 are GCed here!!

Is that correct??

Regards,
Edmen
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Edmen....that's perfect
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edmen!
nice explanation...really I understood this concept very well.

Thanks
Preetha
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edmen,
Thanks for the detailed explanation.
Can anyone please tell me why on last line it will be "GB_8->GB_2" instead of "GB_8->GB_3"
Answer will be same in both cases but just wondering if i left something out(gb1.g is GB_3)

Thanks
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sourabhkapoor,

In the last line we are using gb2.g = gb1.g;. Where gb1 represents to grab3 so gb1.g would be representing grab2. That is why it will assign the value of grab2. grab3 would not be available for garbage collection reason being it is sill accessable from gb1.

I hope this would clear your doubt.
 
Sheriff
Posts: 9691
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
Let me show you how I solve these questions if it helps

 
Sourabhkapoor Kapoor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Salil for the reply.
I still have one question if gb1 is actually representing GARB_3 and therefore gb1.g will be GARB_B2 as explained by you.
Why gb2.g is still referring to GARB_8.Wont it be GARB_7 for same reason?
I am having difficulty understanding this..

Regards,
Sourabh Kapoor.
 
Ankit Garg
Sheriff
Posts: 9691
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
gb2.g is not pointing to GARB_8, gb2 points to GARB_8, gb2.g will point to GARB_2...
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If questions like this are on the exam, so they need to provide pencil and paper.

Ankit, how much time did you take to make this diagram?
 
Ankit Garg
Sheriff
Posts: 9691
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

Leandro Coutinho wrote:If questions like this are on the exam, so they need to provide pencil and paper.


You will be provided with a small plastic slate to write on with a duster to erase it. Its approx the size of a normal A4 size papter

Leandro Coutinho wrote:Ankit, how much time did you take to make this diagram?


So much that Salil gave a reply before me
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic