可以在运行一段时间后获得 Java 中线程使用的实际堆栈大小吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/826206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can one obtain actual stack size used by a thread in Java after some time of running?
提问by Lawrence Dol
The idea would be to help determine the optimum stack size for a given Java application.
这个想法将帮助确定给定 Java 应用程序的最佳堆栈大小。
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 perhapsstack 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.
EDIT2:为了回应一个答案,在每个线程(IIRC)256Kb,我想知道这与所需的现实有多接近(我也想知道这个问题是否可能不是很相关,因为也许堆栈空间是按需分配)。我们有一个基于消息传递和高度线程化的应用程序服务器,可以在从 ARM 手持设备到八核 Linux,再到中端和大型机的所有设备上运行 - 对我们可以在哪里(以及是否)进行交易有一种感觉会很好具有许多消息处理程序的系统上的堆的堆栈空间。
There are some similar questions which are of interest, but they are native/os-specific:
有一些类似的问题很有趣,但它们是本机/操作系统特定的:
回答by Yuval Adam
Stack memory will be hard to obtain.
堆栈内存将很难获得。
Best you can do, and quite easily, is JVM memory usage via MemoryMXBean.
您能做的最好的事情是通过MemoryMXBean.
回答by Cuga
Use JConsole-- you likely already have it: http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.htmlhttp://openjdk.java.net/tools/svc/jconsole/
使用 JConsole——您可能已经拥有它:http: //java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html http://openjdk.java.net/tools/svc/jconsole /
回答by Eldius
You may use the Runtime class to monitor the JVM memmory like this.
您可以像这样使用 Runtime 类来监视 JVM 内存。
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:
或者您可以使用 JConsole、JVisualVM 和 JInfo。您可以在此处获得有关这些工具的更多信息:
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/

