• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Question on Garbage collection

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at the below code.. This is from K&B Q.The question is after line 6 runs how many objects are eligible for garbage collection


And the answer is 1 , I think 0. Please correct me where am I going wrong

I think
1)after m1 method is called both x and mx are referring to the same object,and then mx refers to new object created on line 9.
2)And then this same object returned from m1 is been assigned to x2 on line 4 (i.e mx and x2 are referring to the same object).
3)x4 is new object created on line 5
4)On line 6 x2 refers to the object referred by x ( i.e x and x2 point to the same object).
All the objects created have references to them,so which object is garbage collected
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct me if I am wrong, the object pointed to by x2 before the statement

x2 = x, is unreachable and hence "1" object is eligible for GC.

Thanks
Sashi
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your postulate 1) is incorrect.
After the method call x and x2 refer to different instances of X. Therefore assigning x2 to x will cause 1 instance (the one created in the method) to become elligable for garbage collection.

Remember that Java is pass by value. A new instance is created and returned in the method, the fact that one is passed in as a parameter is a smokescreen to put you on to the wrong idea and it worked with you
The method could have simply been static X m1(){return new X();} as nothing is ever done to mx which will have any consequences to the Object passed to the method.
 
sashi balu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I saw the mistake, I knew the method call was tricky but still fell into the trap.
 
Hey! You're stepping on my hand! Help me tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic