?
Solved

Interesting Quiz

Posted on 2003-03-19
9
Medium Priority
?
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 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