In Java,
String literals in String Constant Pool are not garbage collected, since they are referenced from Table of references which is created by instance of runti
There is a long discussion with real code examples at JavaRanch.
The general output is the following:
The answer to your question is: the only way how you can get OutOfMemoryError because of String constants is to load lot's of classes with many string literals computed at compile time. Then you can eventually exceed maximum size of PermGen space. But this will happen at the time you load classes into memory (e.g., start your application, deploy project to a WebServer, dynamically load new library, etc.)