Wednesday, September 09, 2009

String literals and Garbage Collection in Java

String literals are loaded during class loading. String literal objects are created in heap and a reference to each String literal object is stored in String literal pool. String literals are never garbage collected.
Can this cause OutOfMemory? Not really! The amount of memory required to load String laterals is not more than the size of the class it loaded from. You cannot dynamically create String literals. Unlike other memory leak issues where objects can be created dynamically.

Here is a interesting article on String literals http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

No comments: