?
Solved

Defunct processes left after creating threads on Linux

Posted on 2003-03-21
11
Medium Priority
?
1,113 Views
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.");
                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.
0
Comment
Question by:kmenzel
[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
11 Comments
 
LVL 35

Accepted Solution

by:
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...
0
 

Author Comment

by:kmenzel
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.
0
 
LVL 35

Expert Comment

by:TimYates
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...

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
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.

 

Author Comment

by:kmenzel
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,
Kerry
0
 
LVL 35

Expert Comment

by:TimYates
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 :-)
0
 
LVL 35

Expert Comment

by:TimYates
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...
0
 

Author Comment

by:kmenzel
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.
Thanks
0
 

Author Comment

by:kmenzel
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?

0
 

Author Comment

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

Expert Comment

by:sciuriware
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.
;JOOP!
0
 

Expert Comment

by:CleanupPing
ID: 9058552
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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