I suspect that you're experiencing a slightly different problem- even with the worst coding practices, using String concatenation in a loop shouldn't give you an OutOfMemoryError, it'll just use up...
Type: Posts; User: KevinWorkman
I suspect that you're experiencing a slightly different problem- even with the worst coding practices, using String concatenation in a loop shouldn't give you an OutOfMemoryError, it'll just use up...
Sorry, I actually read it wrong- I didn't realize you were calling new String(string). That actually does create a new instance of String. You can test this by using the == operator on the original...
Your new loop is still allocating a new instance of String each iteration. It has less overhead because it's only using a single instance of StringBuilder, but you're still creating a new String each...
The whole point is that the second loop is allocating a lot more instances, either of String or of a String array depending on which version of the code we're looking at. The first loop is only...
To show what I mean, try this as your first loop:
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i <= 1000000; i++)
{
list.add(new String(line));
}
Well, you're doing two very different things here: in the first loop, you're adding the same instance of String to a List over and over again. That's not going to take up much memory. In the second...
There are a couple misunderstandings here.
The value returned by Runtime.totalMemory() isn't what your program is currently using- it's how much total memory it has had to use. Java allocates...