In Java, you can stumble upon two types of errors in memory:
java.lang.OutOfMemoryError Java heap space error : This exception will be thrown when the application tries to allocate more data to the heap space, but this is not enough room for it. Although there may be a lot of memory available on your machine, you have reached the maximum amount of memory allowed by your JVM, which can be set via the -Xmx optionjava.lang.OutOfMemoryError: Unable to create new native thread occurs when the JVM requests a new thread from the OS. If the main OS cannot allocate a new native thread, this OutOfMemoryError will be selected.
1) Checking the system settings of the Thread system
The file / proc / sys / kernel / threads-max provides a system-wide limit on the number of threads. The root user can change this value if they want:
$ echo 100000 > /proc/sys/kernel/threads-max
You can check the current number of threads running through the / proc / loadavg file system:
$ cat /proc/loadavg 0.41 0.45 0.57 3/749 28174
See the fourth field! This field consists of two numbers separated by a slash (/). The first of these is the number of currently running kernel planning objects (processes, threads); this will be less than or equal to the number of processors. The value after the slash is the number of kernel planning objects that currently exist in the system. In this case, you start 749 threads
2) Check the number of processes per user
In a Linux box, threads are essentially just processes with a common address space. Therefore, you need to check if your OS allows enough processes for the user. This can be checked with:
ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 515005 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 4096 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 10240 cpu time (seconds, -t) unlimited max user processes (-u) 1024 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
The default for each user is 1024 by default. At this stage, we will calculate the number of running processes. The number of running processes can be calculated using the ps output:
$ ps -elf | wc -l 220
However, this number does not take into account threads that may be caused by the process. If you try to start ps using -T, you will also see all the threads:
$ ps -elfT | wc -l 385
As you can see, the number of processes has increased significantly due to threads. This is usually never a problem, but in Java based applications this can cause your system to work within system limits! We continue our investigation. See how many threads are created by your JBoss process. You can do this in at least two ways:
$ ps -p JBOSSPID -lfT | wc -l
The above shell will return the number of lightweight processes created for the process indicated by the PID. This should match the thread dump count generated by jstack:
$ jstack -l JBOSSPID | grep tid | wc -l
You should now have evidence or not, that you need to increase the number of processes for the user. This can be done using the following command:
$ ulimit -u 4096
3) Check PID flow limits
After you have counted the number of threads, you must make sure that you do not press the system limits set by the kernel.pid_max limit parameter. You can verify this value by doing:
$ sysctl -a | grep kernel.pid_max kernel.pid_max = 32768
4) Reduce the thread stack size
Another option that you can use if you cannot change the OS settings reduces the stack size. The JVM has an interesting implementation, thanks to which more memory is allocated for the heap (not necessarily used by the heap), the less memory is available on the stack, and since threads are made from the stack, in practice this means more “memory” in the sense of heap (which people usually say ) leads to the fact that fewer threads can work simultaneously.
First of all, check the default stack size, depending on your operating system:
$ java -XX:+PrintFlagsFinal -version | grep ThreadStackSize intx ThreadStackSize = 1024 {pd product}
As you can see, the default stack stack size is 1024 kb on our computer. To reduce the size of the stack, add the -Xss option to the JVM options. In JBoss EAP 6 / WildFly, the minimum stack stack size is 228kb. You can change it offline by changing JAVA_OPTS, as in the following example:
JAVA_OPTS="-Xms128m -Xmx1303m -Xss256k"
In domain mode, you can configure the jvm element at different levels (Host, Server Group, Server). There you can set the required stack size, as in the following section:
<jvm name="default"> <heap size="64m" max-size="256m"/> <jvm-options> <option value="-server"/> <option value="-Xss256k"/> </jvm-options> </jvm>
Link to the resource:
How to solve java.lang.OutOfMemoryError: unable to create new native thread