1

userText will be a string of around 7000 characters in different languages. I was wondering how the strings will be garbage collected after executing this code. For suppose after unescapeHtml4 userText is assigned a new value and the same thing with after replace.

what happens to the previous string of userText. will they be in string pool or will be removed by the garbage collector.

String userText = context.getRequestParameter( "addedText");
if ( someCondition)
{
   userText = StringEscapeUtils.unescapeHtml4( userText ) );
}
else
{
  userText =  userText.replace( charsequence1, charsequence2 );
}

-- some logic using userText ---

spring 3.0
  • 15
  • 5
  • 2
    7000 chars is pretty much as far away from "large" as it can get. – luk2302 Oct 17 '17 at 17:34
  • @luk2302 what about 6999? Or... 6998 (this could go on a while...) :) – Andy Turner Oct 17 '17 at 17:49
  • @AndyTurner: when 7000 are far away from being large, these smaller numbers are as well. Even multiplying it by ten wouldn’t make it worth thinking about garbage collection. – Holger Oct 18 '17 at 08:01

1 Answers1

1
//This String object will live as long as "context" will live
String userText = context.getRequestParameter( "addedText"); //"addedText" goes to a String Pool
if ( someCondition)
{
//This String object will live as long as "userText" variable is accessible
   userText = StringEscapeUtils.unescapeHtml4( userText ) );
}
else
{
//This String object will live as long as "userText" variable is accessible
  userText =  userText.replace( charsequence1, charsequence2 );
}

Verb "live" means existence of the object before GC will have a right to kill it;-)

Dmitry
  • 87
  • 4
  • so the text from context.getRequestParameter( "addedText") will be in string constant pool as long as the application is up. GC doesn't remove from SCP? – spring 3.0 Oct 17 '17 at 18:11
  • 1
    Dmitry wrote that "addedText" will be there, not `context.getRequestParameter( "addedText")`. – Tom Oct 17 '17 at 18:13
  • oh sorry about that, my doubt is what happens to all the strings in SPC which were from either the one we got from context or which we get from unescapeHtml4 or replace methods. will they be in SPC till the application is running. – spring 3.0 Oct 17 '17 at 18:17
  • 1
    String object goes to the pool only if its String literal like "addedText" or method String.intern() invoked on the string object. Otherwise String object will not be in SP. – Dmitry Oct 17 '17 at 18:25
  • thanks, @Dmitry , that cleared my doubt. some of the StackOverflow accepted answers mentioned that new String() will also create a string object in SCP and in the memory heap. – spring 3.0 Oct 17 '17 at 18:32
  • 1
    It is more likely that you misunderstood what these answers actually wrote. I bet my shoes that they are about something like `new String("someLiteral");` and that literal String goes to the pool. – Tom Oct 17 '17 at 18:34
  • String s1 = "JDK"; - string object in String pool <-----> String s2 = new String("JAVA") - s2 object in heap and String object in pool so the above is how it works, and all other create object only in memory heap. – spring 3.0 Oct 17 '17 at 20:09
  • @spring3.0: *all* strings are in the heap. The pool is just a table of *references*. And even being referenced by the pool doesn’t prevent garbage collection. You need a reference from the code, i.e. using a literal like `"foo"`, to prevent its garbage collection (as long as the code is alive). E.g., calling `intern()` on a string may add it to the pool without preventing its garbage collection. – Holger Oct 18 '17 at 07:58