Java中线程运行一段时间后能否获得实际使用的堆栈大小?
这个想法是为了帮助确定给定 Java 应用程序的最佳堆栈大小。
使用此信息可以做的一件事是创建一个堆栈大小的范围表,线程可以在退出时修改该范围表,并且可以在应用程序退出时定期转储该表。
编辑:这是在具有实际工作负载的客户计算机上运行的情况下,我无法访问分析器。
编辑2:为了响应一个答案,每个线程 (IIRC) 256Kb,我有一段时间以来,我想知道这与实际需要的内容有多接近(我也想知道这个问题是否可能不太相关,因为也许堆栈空间是按需分配的)。 我们有一个基于消息传递和高度线程的应用程序服务器,可以在从 ARM 手持设备到八核 Linux、到中型机和大型机的所有设备上运行 - 如果能够了解我们可以在哪里(以及是否)进行交易,那就太好了具有许多消息处理程序的系统上堆的堆栈空间。
有一些类似的问题很有趣,但它们是本机/操作系统特定的:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
堆栈内存将很难获得。
你能做的最好的事情是通过
MemoryMXBean
。Stack memory will be hard to obtain.
Best you can do, and quite easily, is JVM memory usage via
MemoryMXBean
.使用 JConsole——您可能已经拥有它:
http://java.sun.com/j2se/ 1.5.0/docs/guide/management/jconsole.html
http://openjdk.java.net/tools/svc/jconsole/
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/
我认为您也可以使用全新的 VisualVM 来做到这一点。
I think you can do that with the brand new VisualVM too.
您可以使用 Runtime 类来监视 JVM 内存,如下所示。
或者您可以使用 JConsole、JVisualVM 和 JInfo。
您可以在此处获取有关这些工具的更多信息:
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
You may use the Runtime class to monitor the JVM memmory like this.
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/