>> a.run();
Please don't call run () this way. Please replace this by:
a.start () ;
Mayank.
Main Topics
Browse All TopicsWhy 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();
}
}
};
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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)
{
}
}
}
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();
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?
Business Accounts
Answer for Membership
by: TimYatesPosted on 2003-03-19 at 04:53:04ID: 8166160
Call
a.start()
instead of a.run() in the run() method of ParentThread