java - Garbage collection of String literals -


i reading garbage collection , getting confusing search results when search string literal garbage collections.

i need clarification on following points:

  1. if string defined literal @ compile time [e.g: string str = "java"] garbage collected?

  2. if use intern method [e.g: string str = new string("java").intern()] garbage collected? treated differently string literal in point 1.

  3. some places mentioned literals garbage collected when string class unloaded? make sense because don't think string class ever unloaded.

if string defined literal @ compile time [e.g: string str = "java";] garbage collected?

probably not. code objects contain 1 or more references string objects represent literals. long code objects reachable, string objects to.

it possible code objects become unreachable, if dynamically loaded ... , classloader destroyed.

if use intern method [e.g: string str = new string("java").intern()] garbage collected?

the object returned intern call same object represents "java" string literal. (the "java" literal interned @ class loading time. when intern newly constructed string object in code snippet, lookup , return interned "java" string.)

however, interned strings not identical string literals can garbage collected once become unreachable. permgen space is garbage collected on recent hotspot jvms. (prior java 8 ... drops permgen entirely.)

also treated differently string literal in point 1.

no ... because same object string literal.

and indeed, once understand going on, clear string literals not treated specially either. application of "reachability" rule ...

some places mentioned literals garbage collected when string class unloaded? make sense because don't think string class ever unloaded.

you right. doesn't make sense. sources said incorrect. (it helpful if posted url can read saying ourselves ...)


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -