Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Interesting Quiz

Posted on 2003-03-19
9
Medium Priority
?
382 Views
Last Modified: 2010-03-31
Why does this cause memory leak and How to solve it?


class ChildThread  extends Thread
{
     public void run() {
          System.out.print("a");
     }

};

class ParentThread extends Thread
{

     public void run() {
          for(int i=0; i<10000; i++) {
               Thread a = new ChildThread();
               a.run();
               try
               {
                    a.join();
               }
               catch (Exception e)
               {
               }
          }
     }

     public static void main(String[] args) {
          for (int i=0; i<9000; i++ )
          {
               try
               {
               Thread a = new ParentThread();
               a.start();
               a.join();
               a = null;
               }
               catch (Exception e)
               {
               }
               System.gc();
          }
     }
};
0
Comment
Question by:testn
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8166160
Call

a.start()

instead of a.run() in the run() method of ParentThread
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8166172
>>  a.run();

Please don't call run () this way. Please replace this by:

a.start () ;

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8166185
Oh, Tim Yates gave the same suggestion.... almost same time, so that's why it wasn't there when I loaded this page!

Mayank.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:TimYates
ID: 8166243
I don't think it will cause a memory leak tho,

the problem is more likely to be this:

-------------------

public void run()
{
  for(int i=0; i<10000; i++)
  {
    Thread a = new ChildThread();
    // CALLING RUN DIRECTLY, INSTEAD OF STARTING THIS THREAD
    a.run();
    try
    {
      // NOT SURE WHAT HAPPENS HERE...  I GUESS IT WILL JUST LOCK UP FOREVER, AS THE THREAD IS NOT YET STARTED...
      a.join();
    }
    catch (Exception e)
    {
    }
  }
}


0
 
LVL 9

Accepted Solution

by:
Venci75 earned 150 total points
ID: 8166275
If you create a Thread object without calling its start method - this object will not be garbage collected. This is a java bug/problem and exists in all java versions.

If you don't want to call the start method - use:
class ChildThread implements Runnable
...
and call:
ChildThread ch = new ChildThread();
ch.run();
or
new Thread(ch).start();

0
 
LVL 18

Author Comment

by:testn
ID: 8166706
You guys figured it out so fast!!!! :)

My point is to trick you guys about the things... about Thread. There is obviously a long time bug that hasn't been solved by Sun. This shouldn't be happened since sometimes.... you might create some Thread and maybe an exception is thrown before you have a chance to call start(). Your thread will stay there forever until JVM quits and you will have no chance to refer to that thread anymore (except you refer to it via ThreadGroup if it is in the same ThreadGroup as your current thread. Weird, huh?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8166901
> You guys figured it out so fast!!!! :)

It was glaringly obvious ;-)
0
 
LVL 18

Author Comment

by:testn
ID: 8173534
Hehe, I should hide it out somewhere :P
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question