N
NOBODY
The original statement was generalised - the topic of this forum is
Java so it was assumed that the statement was related to Java, in
which case, it is incorrect.
Is there a particular VM implementation that behaves in that fashion?
No VM implementation that I am looking at causes that statement to
become true (even after being given a context).
I looked at sun's jdk 1.4.2.
Source code show it.
Real time debugging shows it.
Test yourself and you will see it.
Now this is with string buffer. But you may take the string out of the
string buffer and call substring(0, 1), and you will see that you cannot
create more strings, they are still holding the char[] underneath.
import java.util.LinkedList;
public class TestStringBufferToStringHeapCost {
public static void main(String[] args) {
LinkedList ll;
int i = 0;
try {
ll = new LinkedList();
i = 0;
while(true) {
StringBuffer sb = new StringBuffer(100);
sb.append('x');
ll.add(sb.toString());
i++;
if(i%1000 == 0) {
//System.out.println(i);
System.out.print('.');
System.gc();
}
}
} catch(OutOfMemoryError e) {
ll = null;
e.printStackTrace();
} finally {
ll = null;
System.err.println(i);
}
try {
ll = new LinkedList();
i = 0;
while(true) {
StringBuffer sb = new StringBuffer(10000);
sb.append('x');
ll.add(sb.toString());
i++;
if(i%10 == 0) {
//System.out.println(i);
System.out.print('.');
System.gc();
}
}
} catch(OutOfMemoryError e) {
ll = null;
e.printStackTrace();
} finally {
ll = null;
System.err.println(i);
}
}
}
/* because of:
public String (StringBuffer buffer) {
synchronized(buffer) {
buffer.setShared();
this.value = buffer.getValue(); //THIS!!!!!!!!!!!!!!
this.offset = 0;
this.count = buffer.length();
}
}
*/