Garbage collection of String in java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Suppose a string in Java is declared as String s = "hi"

I just want to know if the String object created above will ever be garbage collected?

if i write s = null, at this point will the String object be garbage collected?

Answers

Take it this way, the compiler will move it to the literal pool. Now all the instance of "hi" will be replaced by this value in the pool , which is actually a way compiler optimizes memory usage. No, these values are never garbage collected . However String object created at runtime would be garbage collected.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/12771533/garbage-collection-of-string-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils