Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8262925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:55:25+00:00 2026-06-08T03:55:25+00:00

Is a thread garbage collected when the run() method within the thread is finished

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:55:27+00:00Added an answer on June 8, 2026 at 3:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have created a Thread from within a control to monitor for
Do I need to protect Thread objects from the Garbage Collector? What about the
Is the thread in the onReceive method eligible for garbage collection before it ever
Possible Duplicate: Java Thread Garbage collected or not Consider the following class: class Foo
I have a garbage-collected CoreData app, which is used by a few of my
The documentation for +[NSThread detachNewThreadSelector:toTarget:withObject:] says: For non garbage-collected applications, the method aSelector is
In .NET, after this code, what mechanism stops the Thread object from being garbage
I have a worker thread which reads data repeatedly from an MVar and performs
Thread thread = new Thread(new Runnable() { @Override public void run() { try {
first thread i post here. I have been searching here for answers and almost

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.