Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java thread garbage collection

Is a thread garbage collected when the run() method within the thread is finished or when the function where it was called from finishes?

I don't have an issue or anything, but I want to avoid memory leaks and such.

For people who like code:

public void SomeClass {
    public SomeClass() {

    }

    public void someMethod() {
        Object data = veryBigObject;

        while(true) {
            MyThread thread = new MyThread(veryBigObject);
            thread.run();

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) { /* do nothing */ }
        }
    }

    public static void main(String[] args) {
        SomeClass myclass = SomeClass();
        myclass.someMethod();
    }
}


public class MyThread extends Thread {
    private Object data;

    public Thread(Object data) {
        this.data = data;
    }

    public void run() {
        // do stuff with data
    }
}

In all languages I know, garbage (if the language has any) is collected when a method ends. I assume Java does as well. That means that theoretically the threads I create in someMethod() are not collected until someMethod() ends. If we assume the while loop runs for a very long time, the application would run out of memory and crash.

My question: is this true? If so, what to do to avoid it?

like image 498
Tim S. Avatar asked Jul 13 '12 12:07

Tim S.


People also ask

Do Java threads get garbage collected?

No thread (or the things it refers to) will be garbage-collected while it is still running.

What happens to the thread when garbage collection?

What happens to the thread when garbage collection kicks off? Explanation: The thread is paused when garbage collection runs which slows the application performance. 8.

Does garbage collection block threads?

Blocking during background - background GC does not suspend other threads during Gen2 collections. Nevertheless,Gen0 and Gen1 collections (which are an inevitable part of a full GC) still require managed threads to be suspended.

Is garbage collector a thread?

Java Garbage Collector runs as a Daemon Thread (i.e. a low priority thread that runs in the background to provide services to user threads or perform JVM tasks).


2 Answers

First, in Java GC is non-deterministic. When the object is no longer referenced, it is suitable to be collected in the next GC run, but it is not mandated. Even calling System.gc() is just a suggestion to the GC system.

Second, what counts is references. In each instance of the loop, you overwrite the reference to the thread object. If the threads have finished, there is no need to exit the method to have them collected (of course, while the threads are running they won't be collected, even if your code has no references to it). The issue of exiting the method where the object was created is not relevant.

like image 199
SJuan76 Avatar answered Nov 15 '22 08:11

SJuan76


The GC runs when it is needed (e.g. when you try to allocate memory and the eden space is full)

This can happen on any line of code where you allocate memory (in any thread) This means even if you don't allocate memory in one thread a GC can still occur because memory was allocated in another thread.

You can also trigger a GC explicitly or using a JMX tool like visualvm.

In short: a GC can occur at any time.

like image 41
Peter Lawrey Avatar answered Nov 15 '22 06:11

Peter Lawrey