Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Garbage collection problem

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


when line 7 is reached, how many objects are eligible for garbage collection?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its None. Cause each of the instances are accessible via some reference.
 
Sheriff
Posts: 9692
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
If this is a mock exam question, then please QuoteYourSources...
 
Saloon Keeper
Posts: 13827
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, it's 3.

The go method will create two new objects, which will refer to each other. Then, it will let the argument refer to the one, and the method will return the other.

The main method creates a new object, which is passed to the go method. So this object will now refer to one of the two new objects in the go method, and t2 will now refer to the other object returned from the method.

Then, the method goes out of scope, so all objects become eligible for collection (note that t hides the instance member t).

If the question means that by line 7 the method hasn't gone out of scope yet, then no object is eligible, since t still has a reference to one of them, which has a reference to t2 in go, which has a reference to t1 in go.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I considered that the method has not gone out of scope Thanks Stephan for the explanation.
 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
Then, the method goes out of scope, so all objects become eligible for collection (note that t hides the instance member t).



Stephan, when you say that the method goes out of scope are you considering the "main" method or "go" method?
Because I think that if you are talking about the go method, no objects will be collected but if you are considering the main method going out of scope so I agree.

I'm only asking to understand better this question.

Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 13827
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant the main method.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic