When will a string be garbage collected in java -


in java, when object has got no live reference, eligible garbage collection. in case of string, not case because string go string pool , jvm keep object alive resuse. means string once created 'never' garbage collected?

now in case of string, not case because string go string pool , jvm keep object alive resuse. means string once created 'never' garbage collected?

first, string literals automatically interned / added string pool. strings created application not interned ... unless application explicitly calls string.intern().

second, in fact rules garbage collecting objects in string pool same other strings / other objects. strings garbage collected if ever become unreachable.

in fact string objects correspond string literals typically not candidates garbage collection. because there implicit reference string object in code of every method uses literal. means string reachable long method executed.

however, not always case. if literal defined in class dynamically loaded (e.g. using class.forname(...)), possible arrange class unloaded. if happens, string object literal unreachable, , reclaimed when heap containing interned string gets gc'ed.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -