Can java String literals like "abc"
be garbage collected?. If yes, how can we programatially prove that they are GCed?
Can java string literals be garbage collected?. If Yes, how to prove it?
288 views Asked by TheLostMind At
2
There are 2 answers
0
On
An easy way to proof it, is using a profiler. A profiler gives you the ability to see all objects that are in memory of the application.
Yes, post Java7, String literals can be garbage collected if the class loader which loaded it gets garbage collected and there are no references to the string literal.
Note : In Java -8, you will have to call GC twice in order to ensure that ClassLoaders get GCed (Metaspace.. pfff..Using a different GC won't help).