Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Java Thread have its own process ID?

I want to get the process ID of a Thread to see how much memory it takes.

like image 233
Philipp Spiess Avatar asked Aug 17 '11 09:08

Philipp Spiess


1 Answers

It depends a lot on the OS and how it manages threads. Theoretically it also depends on how the JVM implements threads, but all modern JVMs implement them as native threads.

On Linux each thread will used to get its own process ID, but most tools hide all but one thread per process (i.e. you don't usually see them unless you explicitly ask for them, ps uses the -m flag for example). This is caused by the fact that the Linux kernel doesn't really make much of a difference between threads and tasks.

Edit: as I just learned this is no longer necessarily the case: you can create a thread with the exact same PID as the parent, in which case the threads will be distinguished by different thread IDs.

However since a thread shares its memory with all other threads in the same process, this doesn't help you find out "how much memory a thread takes", since all threads in a process will use the exact same amount (and they all use the same, so the real used memory is shown_memory_use and not shown_memory_user * number_of_threads).

like image 72
Joachim Sauer Avatar answered Oct 14 '22 14:10

Joachim Sauer