• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Garbage Collect and string literal

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

Is string literal "abc" available for Garbage Collect (GC) or not?
Normally objects created in a method are local to the method and are available for GC.
The exception is if a reference to an object created in the method is returned to the caller.
In the method shown above the literal is stored in the String pool and not available for GC according to a mock test.
Is this right ?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, string literals are not garbage collected.
[ April 18, 2003: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it depend on who's garbage collector (or virtual machine) you are using?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Zoetebier:
In the method shown above the literal is stored in the String pool and not available for GC according to a mock test.
Is this right ?


The garbage collection questions that appear on the real exam won't include String constants. Instead, the real exam focuses on the guaranteed behavior of Java. Anything that is implementation dependent is avoided.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Instead, the real exam focuses on the guaranteed behavior of Java.

There do not seem to be many guarantees. In particular see A.2 The Guarantees of GC in this article The Truth About Garbage Collection.
In spite of the lack of guarantees, my impression is we should assume object references in the local variables and the class variables are included in the root set. It sounds like we should not assume string constants are in the root set.
I�ve noticed in more than a few post-exam stories that people are having trouble with garbage collection on the real exam. I wonder why? lack of preparation? lack of understanding? false assumptions? lack of information on gc? bad information on gc? ambiguous or confusing questions?
 
Look ma! I'm selling my stuff!
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic