Garbage Collect and string literal
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ?
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 ?
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Yes, string literals are not garbage collected.
[ April 18, 2003: Message edited by: Barry Gaunt ]
[ April 18, 2003: Message edited by: Barry Gaunt ]
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Would it depend on who's garbage collector (or virtual machine) you are using?
posted 22 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Marlene Miller
Ranch Hand
Posts: 1392
posted 22 years ago
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.Instead, the real exam focuses on the guaranteed behavior of Java.
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 |






