Does a Java Daemon Thread Share Heap or Perm Gen with Spawning Process?

妖精的绣舞 提交于 2019-12-11 10:43:15

问题


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.


来源:https://stackoverflow.com/questions/12389661/does-a-java-daemon-thread-share-heap-or-perm-gen-with-spawning-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!