The String generated for iteration 1 is no longer needed in iteration 2 and its storage space can be freed. I believe that is not happening here.
It definitely is happening.
You're creating 100 million strings, each of which is at least 13 characters - and most of which will be about 20 characters. Each string consists of an object (which has overhead) and a char[]
- so I'd guess at it taking around 60 bytes for a 20-character string.
If garbage collection weren't being effective, 100 million objects requiring 60 bytes each would require 6GB - whereas you're seeing a total memory which is only about 300MB larger than it was to start with.
The strings are being collected - just not immediately.
You haven't told us what you need to do with the strings in your real code (I'm assuming there's a real motivation for this) - assuming you actually need a string in each iteration of a loop, I don't think using StringBuilder
is going to help you. If you only need the data is a StringBuilder
then you can make it a lot more efficient, but it's rare that you create a StringBuilder
but don't call toString
on it.