Increasing stack space of a single worker thread in java

In my java web application, I have one background workflow that requires a lot of stack space because it performs a very complex workflow using the workflow activiti mechanism and groovy script tasks.

Currently, I need to set the JVM Xss value to 16 MB in 64-bit Java and Tomcat to get around any StackOverflowErrors. If an error occurs, the stack trace is really huge (several hundred lines), but it all happens inside the engine, so I can not do anything about it.

Now my question is: is there a way to increase the stack size of a single thread at runtime?

I would like to lower the Jss default Xss settings for all threads in the application to 512 thousand, which, as far as I know, is enough, and only run the working one from 16 M.

The Java API provides some information on this for the constructor of the Thread class:

public Thread(ThreadGroup group, Runnable target, String name, long stackSize) 

but he mentions that behavior is not guaranteed ([1]), and I did not find any information if it worked on windows.

Also, if the stack space of a single thread cannot be increased, and I must specify a value of 16 MB as the default value, what would be the consequences of such a high value? Does this mean that each new thread reserves 16 MB of memory during initialization (i.e. 200 threads * 16 MB: 3.2 GB)?

As far as I can tell from jconsole and taskmgr, the amount of memory doesn't seem to change much with the increase in 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)

+6
source share
2 answers

It costs nothing that -Xss on HotSpot indicates the maximum size, and not the size that it will use. The size used is based on usage, so if you specify that it is unreasonably large, it will waste virtual memory (which may be a problem for the 32-bit JVM), but it will not waste physical memory. If you have a 64-bit JVM, you have little flaw in making the largest one you want any thread to.

Also, if the stack space of a single thread cannot be increased, and I must specify a value of 16 MB as the default value, what would be the consequences of such a high value? Does this mean that each new thread reserves 16 MB of memory during initialization (i.e. 200 threads * 16 MB: 3.2 GB)?

Each thread will use this large virtual memory. On a 32-bit JVM, you run out of address space, even if you are actually using very little memory. On a 64-bit JVM, your restrictions are in TB, and only the stack actually used will use main memory.

+4
source

If the corresponding thread is very intense in the process. Implement this thread logic as another application and pass information and process variables using queues. You can increase the memory settings of this application according to your needs.

From the Documents link:

The virtual machine can treat the stackSize parameter as a sentence. If the specified value is unreasonably low for the platform, the virtual machine may instead use some platforms with the minimum value; if the specified value is unreasonably large, the virtual machine may instead use some maximum platform size. Similarly, a virtual machine can freely traverse a specified value up or down as it sees fit (or completely ignore it).

0
source

Source: https://habr.com/ru/post/920601/


All Articles