Today I was just messing around with Threads in Java. After some tests, I decided to create & run 10,000 Threads and see if everything goes well or if some unexpected behavior will happen. Indeed, everything did go well, but I noticed something.
I spawned 10,000 Threads. My Memory usage was, as expected, over 1gb. But after every Thread was finished, the Memory usage dropped from 1gb to 200mb. So there is still ~180mb of memory which seems to be still busy. But why is that? Every Thread is finished, I double checked that.
Here is my test-code I have written. The program will spawn the threads, wait 10seconds and then the Thread should be finished. The BufferedReader is for the waiting, so the program will not instantly close.
public static void main(final String[] args) throws IOException {
final long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
new Thread(new Runnable() {
public void run() {
while (System.currentTimeMillis() - startTime < 10000) {
System.out.println(System.currentTimeMillis() - startTime);
}
}
}).start();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();
}
Anyone have some ideas on this? Is there something I'm missing?