c# - Does the CLR/JVM keep one single intern pool for all running .net/java apps? -
the following extract msdn:
the common language runtime conserves string storage maintaining table, called intern pool, contains single reference each unique literal string declared or created programmatically in program. consequently, instance of literal string particular value exists once in system.
for example, if assign same literal string several variables, runtime retrieves same reference literal string intern pool , assigns each variable.
the intern method uses intern pool search string equal value of str. if such string exists, reference in intern pool returned. if string not exist, reference str added intern pool, reference returned. .... if trying reduce total amount of memory application allocates, keep in mind interning string has 2 unwanted side effects. first, memory allocated interned string objects not released until common language runtime (clr) terminates.
so, mean clr keeps 1 single intern pool running .net apps? example: if program creates string literal "test" , if program tries create string literal "test", same copy used? same question applies jvm.
the clr keeps intern pool per instance. if read further down msdn link:
if trying reduce total amount of memory application allocates, keep in mind interning string has 2 unwanted side effects. first, memory allocated interned string objects not released until common language runtime (clr) terminates.
for java it's per jvm start.
however according this article:
this myth goes in opposite direction of myth 2. people belive internalized strings stay in memory until jvm ends. may have been true long time ago, today internalized strings garbage collected if there no more references them. see below modified version of program above. clears references internalized strings time time. if follow program execution jconsole, see permgen space usage goes , down, garbage collector reclaims memory used unreferenced internalized strings.
which means in java interned strings can gced.
Comments
Post a Comment