Increasing stack space of a single worker-thread in java(增加java中单个工作线程的堆栈空间)
问题描述
In my java web application, I have a single background-worker thread, which requires a lot of stack space, because it runs a really complex workflow using the activiti workflow engine and groovy script tasks.
Currently I need to set the JVM Xss setting as high as 16MB on a 64bit Java and Tomcat, to circumvent any StackOverflowErrors. If the error occurs the stack trace is really huge (several hundred lines long) but it all happens inside the engine, so I can't really do anything about it.
Now my question is: is there a way to increase the stack size of a single thread at runtime?
I'd like to lower the JVMs default Xss settings for all threads in the application to 512k, which I know is enough and only run the worker with 16M.
The Java API provides some information on this topic for a constructor of the Thread class:
public Thread(ThreadGroup group,
Runnable target,
String name,
long stackSize)
but it mentions, that the behaviour is not guaranteed ([1]) and I didn't find any information, if it would work on windows.
Also, if the stack space of one thread cannot be increased and I have to specify 16MB as default value, what would be the consequences of having such a high setting? Would that mean that every new thread would reserve 16MB of memory upon initialization (i.e. 200 threads * 16MB: 3,2 GB)?
As far as I can tell from jconsole and taskmgr, the memory footprint doesn't seem to have changed much since the increase of the Xss settings, but maybe I'm missing something.
Any help or clarification would be appreciated.
[1]: http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String, long)
Its worth nothing that -Xss
on HotSpot specifies the maximum size, not the size it will use. The size used is based on usage so if you specify this unreasonably large it will waste virtual memory (which can be a problem on a 32-bit JVM) but it won't waste physical memory. If you have a 64-bit JVM, there is little disadvantage in making the largest you want any thread to be.
Also, if the stack space of one thread cannot be increased and I have to specify 16MB as default value, what would be the consequences of having such a high setting? Would that mean that every new thread would reserve 16MB of memory upon initialization (i.e. 200 threads * 16MB: 3,2 GB)?
Each thread will use this much virtual memory. On a 32-bit JVM, you will run out of address space even if you actually use very little memory. On a 64-bit JVM, your limits is in the TB and only the stack actually used will use main memory.
这篇关于增加java中单个工作线程的堆栈空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:增加java中单个工作线程的堆栈空间
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01