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


Defunct processes left after creating threads on Linux

Posted on 2003-03-21
Medium Priority
Last Modified: 2012-06-21
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.");
                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();
            System.out.println("Created a new thread " + (num++) + " from main.");
            try {
                System.out.println("Main thread sleeping for 15sec.");
            } catch (InterruptedException ex) {
                System.out.println("My sleep was interrupted. " + ex);
    public static void main(String Args[]) {        
        new TestThread();

Thanks for the help.
Question by:kmenzel
LVL 35

Accepted Solution

TimYates earned 2000 total points
ID: 8184233
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...

Author Comment

ID: 8184316
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.
LVL 35

Expert Comment

ID: 8184504
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...


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!

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8185392
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,
LVL 35

Expert Comment

ID: 8185883
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 :-)
LVL 35

Expert Comment

ID: 8185887
> 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...

Author Comment

ID: 8190065
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.

Author Comment

ID: 8197499
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?


Author Comment

ID: 8264206
Anybody else have any experience with defunct processes on Linux?
LVL 24

Expert Comment

ID: 8267559
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.

Expert Comment

ID: 9058552
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 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

580 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