Can one obtain actual stack size used by a thread in Java after some time of running?

旧城冷巷雨未停 提交于 2019-11-26 23:20:58

问题


The idea would be to help determine the optimum stack size for a given Java application.

One thing that could be done with this information create a range-table of stack sizes which the threads could modify as they exit and which could be dumped periodically and at application exit.

EDIT: This is in the context of running on customer machines with real workloads which I can't get profiler access to.

EDIT2: In response to one answer, at (IIRC) 256Kb per thread, I have wondered for a while now how close that is to the reality of what's needed (I also wonder if this question might be not very relevant because perhaps stack space is allocated on demand). We have an application server which is based on message passing and highly threaded and runs on everything from an ARM handheld to octo-core Linux, to midrange and mainframes - it would be good to have a feeling for where (and if) we can trade stack space for heap on systems with many message handlers.

There are some similar questions which are of interest, but they are native/os-specific:

  • How to determine maximum stack usage?
  • How to determine optimal thread stack size?

回答1:


Stack memory will be hard to obtain.

Best you can do, and quite easily, is JVM memory usage via MemoryMXBean.




回答2:


Use JConsole-- you likely already have it: http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html http://openjdk.java.net/tools/svc/jconsole/




回答3:


I think you can do that with the brand new VisualVM too.




回答4:


You may use the Runtime class to monitor the JVM memmory like this.

Runtime rt = Runtime.getRuntime();
System.out.println((rt.freeMemory() / 1024) + "/" + (rt.maxMemory() / 1024) + " kB");

Or you may use JConsole, JVisualVM e JInfo. You may get more information about these tools Here:

http://java.sun.com/developer/technicalArticles/J2SE/monitoring/



来源:https://stackoverflow.com/questions/826206/can-one-obtain-actual-stack-size-used-by-a-thread-in-java-after-some-time-of-run

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