Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the entire Xss (stack space) used for each Java thread?

Tags:

java

stack

regex

I am considering increasing the stack size to work around the StackOverflowError thrown by the regex library which does not appear to be on the plans for a fix.

Edit: Solution

  • Stephen C's answer is probably the best answer to the problem, even if it is not an answer to the question. Although my string size was more than 4k already, I was still likely to eventually have the problem again during the lifetime of the product
  • aioobe's answer is the best answer to the actual question, perhaps not the actual problem.
  • Chris's answer is a very good idea. Edit: JRegex worked great!
like image 688
700 Software Avatar asked Dec 09 '10 20:12

700 Software


People also ask

Does each thread in Java uses separate stack?

Within Java, the Java threads are represented by thread objects. Each thread also has a stack, used for storing runtime data. The thread stack has a specific size.

Is stack size fixed in Java?

If we don't specify a size for the stacks, the JVM will create one with a default size. Usually, this default size depends on the operating system and computer architecture. For instance, these are some of the default sizes as of Java 14: Linux/x86 (64-bit): 1 MB.

How much memory does a Java thread use?

By default in 64-bit JVMs, this is 1MB. That doesn't mean each thread will actually consume 1MB of physical resources. All it means is the JVM will malloc 1MB for each thread for its stack. All modern operating systems lazily allocate physical ram.

How many stacks can a thread have in Java?

There is one stack per process, which gets subdivided into non-overlapping parts, one substack per thread.


1 Answers

Is the entire Xss (stack space) used for each Java thread?

According to this page, yes:

  • increase the stack size for all threads in your application, by including -Xssnnm in the Java command line (where nn is the number of megabytes of stack space per thread);

You can however choose a larger stack size for a specific thread using the Thread(ThreadGroup group, Runnable target, String name, long stackSize) constructor.

Allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group, and has the specified stack size.

Note however that (according to documentation) the effect of the stackSize parameter, if any, is highly platform dependent and that the value of the stackSize parameter may have no effect whatsoever on some platforms.

like image 191
aioobe Avatar answered Oct 05 '22 11:10

aioobe