1

I am trying to ascertain the best possible approach of figuring out the impact of threads in wait state on a java process (more specifically memory and not cpu). Any suggestions that would help me figure out their impact (possibly via jvisualvm/jconsole etc) will be greatly appreciated.

Update: Current thread count is a few hundred in wait state - approx 500. I am trying to figure out the best way of checking whether it can/will have any potential impact on GC in old generation.

4

2 回答 2

3

线程的内存使用在概念上可以分为:

  • 线程堆栈(我相信默认为 2MB,但可以使用 -Xss VM 选项和/或在 Thread 构造函数中指定)

  • java 线程对象和关联对象(位于 VM 堆中)。对于给定的实现几乎是恒定的。只有几个 KB。

  • 本机开销 - 管理线程所需的内核内存。应该可以忽略不计(几 KB)。

  • 由线程管理的用户数据(可通过其线程对象或局部变量访问的数据)。可以变化很大。

前三个元素很容易测量(它们对于 VM 实例实际上是恒定的,随线程数线性扩展),最后一件事完全取决于线程代码/数据。

由于堆栈大小通常在很大程度上决定了每个线程的成本(与内核开销和线程对象相比),等待线程的内存影响可以简化为它的堆栈大小。

对于虚拟内存系统,这只是虚拟影响(分配的地址空间),而不一定是分配的物理内存量(永远不会为未使用的堆栈空间分配物理内存页面)。当您创建许多线程时,32 位系统会很快耗尽地址空间(例如:1000 个线程乘以 2MB 堆栈大小 = 2GB)。

于 2013-10-22T16:10:45.017 回答
2

I am trying to ascertain the best possible approach of figuring out the impact of threads in wait state on a java process (more specifically memory and not cpu)

The affect of a single thread is going to be extremely minimal. Each thread gets allocated a certain amount of stack-space memory that is allocated outside of the heap. I believe typically this is 512k or 1M for 64-bit machines. This can be customized with the -XX:ThreadStackSize=YYY JVM argument where YYY is the size of the stack in kilobytes.

There also is the Thread object itself on the heap as well as the various accounting data kept by the JVM. Certainly any objects that the thread owns also need to be taken into account.

The only time you worry about this space is if you plan on having 1000s of threads or have very limited memory constraints.

于 2013-10-22T15:49:00.170 回答