• 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 wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Garbage Collection

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me after which line does the object created at line 1 is eligible for garbage collection and why?

 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not. It's a String literal which is interened in a 'String Pool'. Also the String on line three will be the same == object.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not sure -- but my guess is after line 7 i think seeing as there are 2 versions of str of which one hides the other.
Therefore -- there are 2 references to the string "ABC".
The 1st reference gets set to null al line 6 with str = null.
The 2nd reference gets set to null with t.str = null.
After this line there are now no more reference to "ABC".

Is this correct anyone?
 
donal horgan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry -- that should read lines 5 & 6 -- not 6 & 7.
 
John Campbell
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers guys but won't it be correct to say that the object itself is set to null on line 7 thereby making itself and all its variables (including String ones) eligible for garbage collection?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

after which line does the object created at line 1 is eligible for garbage collection and why


No object is created on line 1. str is an instance String reference variable and is part of the Test object created on line 2. The string literal "ABC" was already in the string literal pool when main() was called and it definitely cannot be garbage collected as long as class Test is reachable in memory.

One JavaRancher has asserted that the life of the string literal pool is associated with the life of the class loader. Since most programs use the jvm's class loader, that would mean that "ABC" will stay in memory until the jvm terminates. Since the gc is called by the jvm, that would mean that string literal "ABC" will never be garbage collected.
[ March 24, 2005: Message edited by: Mike Gershman ]
 
John Campbell
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it, thanks for your help!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic