Java thread garbage collection

Is thread garbage collected at the end of the run() method on the thread, or when the function from which it was called ends?

I have no problem or anything else, but I want to avoid memory leaks, etc.

For people who like the 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 ​​that I know, garbage (if any language is) is collected when the method ends. I assume Java works too. This means that theoretically, the threads I created in someMethod() are not collected until the end of someMethod() . If we assume that the while loop has been running for a very long time, the application will run out of memory and crash.

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

+4
source share
5 answers

Firstly, in Java, GC is not deterministic. When an object is no longer referenced, it can be collected in the next GC session, but it is not assigned . Even calling System.gc() is just a suggestion from the GC system.

Secondly, with regard to links. In each case of the loop, you overwrite the reference to the stream object. If the threads are finished, there is no need to exit the method to collect them (of course, while the threads are running, they will not be collected, even if your code does not have references to it). The problem of exiting the method in which the object was created does not matter.

+6
source

GC starts when necessary (for example, when you are trying to allocate memory and the edan space is full)

This can happen on any line of code where you allocate memory (in any thread). This means that even if you do not allocate memory in one thread, GC can still happen because the memory was allocated in another thread.

You can also start GC directly or using a JMX tool like visualvm.

In short: GC can happen anytime.

+3
source

Each instance of MyThread will be marked as compiled by the GC immediately after starting another loop iteration. If you want to start MyThread as a thread, you need to use the start () method!

In the case when you use the start () method, MyThread will be marked as compiled by GC immediately after starting another loop iteration and! end of run () method.

But there are no guarantees as to when it will be assembled by GC. This is a feature of GC.

+1
source

It was found that it will be called in the stream after the task is completed and no longer use the data , but not when the garbage collector is automatically called by the virtual machine periodically or when some events occur.

So, when the stream was completed and the data became unused, you cannot know whether it was automatically collected or not.

You can also call it with: System.gc() .

If your stream was interrupted, and its point data is no longer used, after an explicit call it will be provided that it was collected.

0
source

In the code you posted above, not a single thread runs in parallel. To create a new thread, you must call the .start() method, which internally calls the .run() method for Thread. Everything is working sequentially right now. This way your thread.run () will complete execution, your current thread will sleep, and your while will continue the loop and do it again and again.

A GC can go anywhere, and others claim that thread threads ( that were started using start() ) will only be GCed if they have completed their execution.

0
source

All Articles