Question

I'm running this extremely simple program:

class helloWorld {
    public static void main(String[] args) throws InterruptedException {

    while(true) {
        System.out.println("Hello World!");
        Thread.sleep(1000);
        }
    }
}

And I don't understand why the java virtual machine allocates so many processes (they have different PID), has you can see here (htop_java_helloworld.png):

<code>htop</code> output showing multiple java processes

I even suspected of the instruction Thread.sleep(1000) but the strange behavior doesn't change if it's removed.

UPDATE

I'm sorry I forgot to mention some useful details:

  • the system is Debian GNU/Linux Jessie
  • the JVM installed is from the package openjdk-7-jdk
  • the output is from htop, I have filtered it with \java so only useful processes are listed

I compiled the source code written at the beginning of the question with the command javac helloWorld.java then I run it with the command java helloWorld.

I run only one instance of the program and all those processes are allocated, when I kill it with ctrl+c all the processes listed disappear, so there aren't more instances of the program running at the same time.

Était-ce utile?

La solution

It is unclear to me what these are. (I don't recognize the utility that you are using to display the processes. If this was top -H output, I'd understand it. UPDATE ... htop, most likely.)

One possibility is that you have run the application multiple times ... without killing the old ones ... and you are seeing all of the processes.

Another possibility is that you are seeing threads not processes. On Linux, each native thread gets its own unique PID. Classically, there would have been 3 threads created, one to run your application, and a couple for the garbage collector and the finalizer. But in your case, the JVM could have created multiple GC threads ... for parallel garbage collection.

Why does java allocate so many processes to run a simple “hello world”?

Assuming that you are seeing threads ... the reason that the JVM is pre-creating GC threads is that it would be problematic to create them when the JVM needs them to be available; i.e. when the heap fills up.

(The JVM is optimized for running big, long-running applications, not trivial ones like "hello world". It is well known that Oracle JVMs are not good for running small short-lived applications, because of JVM startup overheads, heap usage and so on.)

Autres conseils

The actual reason for this is the implementation of the system threads library in Linux. Conceptually a single main program run results in a single process with few threads (garbage collector etc). However Linux threads are implemented as cloned process, hence each java thread shows up in its own process. Apart from this, as others too have pointed out, there could be other reasons related to your running of program multiple times without the processes being ended.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top