Java8字符串垃圾收集

Java8字符串垃圾收集,java,string,garbage-collection,jvm,Java,String,Garbage Collection,Jvm,userText将是一个不同语言的大约7000个字符的字符串。 我想知道在执行这段代码之后,字符串将如何被垃圾收集。例如,假设在unescapeThml4 userText之后分配了一个新值,与after replace相同 上一个userText字符串会发生什么情况。它们是在字符串池中还是将由垃圾收集器删除 String userText = context.getRequestParameter( "addedText"); if ( someCondition) { userText

userText将是一个不同语言的大约7000个字符的字符串。 我想知道在执行这段代码之后,字符串将如何被垃圾收集。例如,假设在unescapeThml4 userText之后分配了一个新值,与after replace相同

上一个userText字符串会发生什么情况。它们是在字符串池中还是将由垃圾收集器删除

String userText = context.getRequestParameter( "addedText");
if ( someCondition)
{
   userText = StringEscapeUtils.unescapeHtml4( userText ) );
}
else
{
  userText =  userText.replace( charsequence1, charsequence2 );
}
--使用用户文本的一些逻辑---


动词“live”表示GC有权杀死该对象之前该对象的存在;-)

7000个字符离“大”差不多远了。@luk2302 6999怎么样?或6998(这可能会持续一段时间…)@AndyTurner:当7000远远不是大数字时,这些较小的数字也是大数字。即使将其乘以10也不值得考虑垃圾收集。因此,只要应用程序启动,context.getRequestParameter(“addedText”)中的文本将位于字符串常量池中。GC没有从SCP中删除?Dmitry写道“addedText”将在那里,而不是
上下文。getRequestParameter(“addedText”)
。哦,很抱歉,我怀疑SPC中的所有字符串会发生什么情况,这些字符串来自我们从上下文获得的字符串,或者来自UnescapeThml4或replace方法。在应用程序运行之前,它们将一直在SPC中。只有在字符串对象上调用其字符串文字(如“addedText”或方法String.intern())时,String对象才会进入池。否则字符串对象将不在SP中。很可能您误解了这些答案的实际内容。我敢打赌,它们是关于
newstring(“someLiteral”)
和该文本字符串进入池。
//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 );
}