2

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.

Manuel Durando
  • 1,493
  • 3
  • 19
  • 30
  • You OS is that? Really cool.. – gangadhars May 05 '14 at 07:25
  • I don't get that behavior. Are you running it multiple times without killing it (since the program never dies)? Then it would show up however many times you ran it. – Jared May 05 '14 at 07:28
  • If you show the output of a program, say what the program is. Right now, we have to guess it's `htop` from the *filename* of the picture you linked to. Pretty tenuous... – T.J. Crowder May 05 '14 at 07:45
  • All: You may be tempted to post an answer (as I did) saying "I don't get that result" -- before you do, make sure you're using a \*nix-like system, and check with this command: `top -H -c -p $(pgrep -d',' -f java)` Then see Stephen C's answer. :-) – T.J. Crowder May 05 '14 at 07:47
  • @all I updated the question, I'm sorry for the missing details in the original version. – Manuel Durando May 05 '14 at 10:17

2 Answers2

5

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • +1 Your GC theory is supported at least partially by the sequential nature of the pids in the picture... – T.J. Crowder May 05 '14 at 07:40
  • *"I don't recognize the utility that you are using to display the processes. If this was top -H output, I'd understand it ..."* The filename of the picture, when I downloaded it from the shortening service, was `htop_java_helloworld.png`, which is suggestive... :-) – T.J. Crowder May 05 '14 at 07:42
  • All: The easy way to see this in your own testing is to use a \*nix-like system, fire up the program, and then in another window issue this command: `top -H -c -p $(pgrep -d',' -f java)` – T.J. Crowder May 05 '14 at 07:48
  • Thank you for your answer. I updated the question with the missing information. Anyway I only run one instance of the program at the time and then I successfully kill it: all the processes listed belong to the same single instance of the running program. – Manuel Durando May 05 '14 at 10:24
4

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.

Shailendra
  • 8,874
  • 2
  • 28
  • 37
  • 1
    Thank you, this question and its replies also helped me a lot http://stackoverflow.com/questions/4517301/difference-between-pid-and-tid – Manuel Durando May 05 '14 at 14:05