Defunct processes left after creating threads on Linux

I am having trouble with defunct processes being left around from my java program when it runs on Linux 7.3. I have a main thread that runs until the app is shut down. It spawns child threads that live for a short while. The main thread spawns the children every so often. Problem is, when each child finishes from the run call, it leaves a defunct process. The processes don't get cleaned up until the main app dies. How can I get the children to stop leaving the defunct process. I understand that in C, the parent needs to wait for the child so that it can get it's return status. I don't want to have to wait, but I have tried doing a join() after the child died too.

Here is a small program I made to illustrate this:

public class TestThread {    
    class ChildThread extends Thread  {
     public ChildThread() {     }
        public void run() {
            try {
                System.out.println("New Child Thread created and sleeping for 5sec.");
                this.sleep(5000);
                System.out.println("Thread exiting.");
            } catch (InterruptedException ex) {
                System.out.println("My sleep was interrupted. " + ex);
            }
        }
    };

    /** Creates a new instance of TestThread */
    public TestThread() {
        int num = 1;        
        while (true) {
         ChildThread aThread = new ChildThread();
         aThread.start();
            System.out.println("Created a new thread " + (num++) + " from main.");
            try {
                System.out.println("Main thread sleeping for 15sec.");
                Thread.sleep(15000);
                aThread.join();
            } catch (InterruptedException ex) {
                System.out.println("My sleep was interrupted. " + ex);
            }            
        }
    }
   
    public static void main(String Args[]) {        
        new TestThread();
    }    
}

Thanks for the help.
kmenzelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
From: http://java.apache.org/faq/fom-serve/cache/274.html

"If you are running Linux with native threads, the "ps" command lies to you.  Each
of those "processes" is actually a thread in the existing JVM. To prove it, just
do a normal shutdown and they will all disappear.  In fact, the number of new
threads per reload depends on your configuration parameters -- some people have
reported seeing five or six new threads per iteration.

So why do you get new threads all the time?  I suspect there's a problem in the
JVM, garbage collecting old  thread occurrences."

So it looks like it is part Java's fault, and part the fact that "ps" shows threads as processes, which they are not...

It shouldn't be a problem...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kmenzelAuthor Commented:
They may be false, but they are filling up the process table. The app I made should run as long as the machine is up. There could be hundreds of thousands of these defunct processses over time.
0
TimYatesCommented:
From: http://www.jguru.com/faq/view.jsp?EID=143462

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

Both green and native threads are mechanisms to support multithreaded execution of Java programs. Some JDK distributions (such as Blackdown's) include the option to run with either type of threading.
Native threads use the operating system's native ability to manage multi-threaded processes - in particular, they use the pthread library. When you run with native threads, the kernel schedules and manages the various threads that make up the process.

Green threads emulate multithreaded environments without relying on any native OS capabilities. They run code in user space that manages and schedules threads; Sun wrote green threads to enable Java to work in environments that do not have native thread support.

There are some important differences between using the two in a Linux environment:

Native threads can switch between threads pre-emptively, switching control from a running thread to a non-running thread at any time. Green threads only switch when control is explicitly given up by a thread (Thread.yield(), Object.wait(), etc.) or a thread performs a blocking operation (read(), etc.).

On multi-CPU machines, native threads can run more than one thread simultaneously by assigning different threads to different CPUs. Green threads run on only one CPU.

Native threads create the appearance that many Java processes are running: each thread takes up its own entry in the process table. One clue that these are all threads of the same process is that the memory size is identical for all the threads - they are all using the same memory.

Unfortunately, this behavior limits the scalability of Java on Linux. The process table is not infinitely large, and processes can only create a limited number of threads before running out of system resources or hitting configured limits.

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

So:  If it is a multi-processor machine that you are going to be running on, then you may want to reconsider your application design, as using the Native threads will fill your processor table...and using Green threads will not make use of the multiple processors...

BUT!

I have been doing some more searching, and I thin k that you can't use Green threads on Linux any more...

Are you using the latest version of java?

Sun reckon this has been fixed in the latest version...

Good luck!

TIm.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

kmenzelAuthor Commented:
I'm using JDK 1.4.1 on Linux 7.3. I will be deploying my app no many versions of Linux.

I am surprised. I thought I was doing something wrong in my app.

Thanks for the info,
Kerry
0
TimYatesCommented:
Maybe try IBM's JDK http://www-106.ibm.com/developerworks/java/jdk/linux140/

And make sure you have the latest version of the glibc libraries on your linux box...

good luck with it :-)
0
TimYatesCommented:
> I thought I was doing something wrong in my app.

As I said, I would probably look at changing the way the app works...  Have threads which go to sleep rather than die, and get re-created or something...

Maybe if you add them to a ThreadGroup, java will handle the processes better...
0
kmenzelAuthor Commented:
Not sure I can change the app to not have any threads that die. Difficult at best. I may be able to put them in a thread group. Will try that Monday.
Thanks
0
kmenzelAuthor Commented:
I tried using the latest IBM JDK and tried putting the child threads in a thread group. Neither one helped. I'm still getting defunct processes when the children die.

Any other suggestions besides making immortal children?

0
kmenzelAuthor Commented:
Anybody else have any experience with defunct processes on Linux?
0
sciuriwareCommented:
I'm running JDK 2 1.4.1_02 on SuSE 7.3 and I can see no defunct processes left.
But, when the app is running I see various processes.
Are you confused by this picture (ps)?
Just don't worry about that.
;JOOP!
0
CleanupPingCommented:
kmenzel:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.