Java: Garbage Collector doesn’t collect finished thread

I got a problem which is actually not new and discussed in many different platforms. Nevertheless, I can’t find an answer to my problem, maybe I’m simply too stupid to fix this problem – but I hope you guys can help me out here…

In short: I have a thread which is finished running, but not garbage collected.

In long: See following example code:

public void saveSomething() {
    Thread thread = new Thread(new Runnable() {

         public void run() {
             // heavy memory access (~400 MB), finishes after ~10sec

Okay, so this thread gets started and finished after some time. During this time a lot memory is used, which is okay. But my problem is:

After the thread finished, the memory is not set free

And I don’t know how I can ensure that.
Finished threads, which are not used anymore, should get garbage collected as far as I’m informed; but this doesn’t seem to happen here 🙁

See this screenshot from jvisualVM:

enter image description here

1: I trigger the thread start by calling saveSomething()

2: Thread was finished long ago (I saw it by debugging), and I pressed “Perform GC” in jvisualvm

As you can see, after I force the GC, everything is working like I want. But this has to happen automatically. How can I do that, what am I doing wrong?

If you need more infos, please ask.
Note: It seems that after a day (hopefully shorter) the memory is back to normal, maybe the GC is just “slow” or rather not very frequently timed?

Source: java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.