The Artima Developer Community
Sponsored Link

Java Answers Forum
String Literal Pool

3 replies on 1 page. Most recent reply: Jun 17, 2002 8:58 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
James

Posts: 2
Nickname: jaime2k
Registered: Jun, 2002

String Literal Pool Posted: Jun 16, 2002 6:37 PM
Reply to this message Reply
Advertisement
When creating Strings..they say its always better to use
String s = "s" because its not using the "new" construct..But as what i recently found out...using the first method will place "s" in the String literal pool..and string's on the literal pool aren't gc'ed. This could cause OutOfMemoryException specially if your application is running 24x7. Is this true? Anyone could shed light on this?


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: String Literal Pool Posted: Jun 16, 2002 8:50 PM
Reply to this message Reply
Suggest you set your string s to null in your program when you no longer need it so it will be "suggested" to the java virtual machine for garbage collection.

James

Posts: 2
Nickname: jaime2k
Registered: Jun, 2002

Re: String Literal Pool Posted: Jun 17, 2002 12:32 AM
Reply to this message Reply
String s = new String("a string");
s = null;

Yep the VM would garbage collect that and that
string wouldn't go to the literal pool...but it is said
that Strings in the literal pool isn't GC'ed such as

String a = "some string";

So is there some truth to this?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: String Literal Pool Posted: Jun 17, 2002 8:58 AM
Reply to this message Reply
But of course the only strings in the literal pool would be the strings you literally typed (well, unless you are using a code geneator). This is likely to be a small amount of memory compared to what the computer has, unless you are one hell of a typist.

By the way, one of the advantages of this literal pool is that it saves memory. In this scenario:
   String s = new String("Go placidly amid the noise and haste...");
   String x = new String("Go placidly amid the noise and haste...");

You have two distinct and separate copies of the same string gobling up defenselsss little memory bits by the hundreds. While in this one:
   String s = "Go placidly amid the noise and haste...";
   // ... Several gigabytes of painstaking hand-typed code later...
   String x = "Go placidly amid the noise and haste...";

Both s and x refer to the same String, leaving vast expanses of bored memory circuits with nothing to do.

Flat View: This topic has 3 replies on 1 page
Topic: about security manager in applet/tomcat Previous Topic   Next Topic Topic: Using BasicFileReader with text file, please help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use