Does a Java Daemon Thread Share Heap or Perm Gen w

2019-09-07 11:43发布

问题:

If I create a Daemon thread from my program (a non-daemon process), are the heap and perm gen memory spaces shared with the new thread or is it allocated anew?

If the daemon thread gets its own spaces, are the JVM memory tuning args like max heap size, etc respected in the creation of the new thread?

回答1:

are the heap and perm gen memory spaces shared with the new thread or is it allocated anew?

All threads (daemon status does not matter) share heap and perm memory spaces. Each thread has it's own stack space which it uses to store method fields and the call stack. You can tune the size of the allocated per-thread stack space by changing JVM arguments. But even these stack areas are part of the general JVM heap space.

Threads also have a memory cache when it is running in a separate CPU. The per-CPU memory cache is used for performance reasons so updates can be made to local CPU memory for speed reasons without having to synchronize the information to central storage on every access. But these caches still read from and write to the general JVM memory space.

For more information about what daemon-thread really means, see @Peter's answer.



回答2:

The ONLY significant difference between a daemon thread and a normal thread is whether it will prevent the process from being stopped. A normal thread keeps the JVM running, a daemon does not.

Daemon threads have

  • the same heap which has one maximum size.
  • the same perm gen which also has one maximum size.
  • the same thread priorities.
  • use the same amount of memory.
  • the same maximum size for a stack.

BTW: Other differences to be pedantic,

  • isDaemon() will return true
  • a daemon will create daemon threads by default.
  • will appear as a daemon in a thread dump.