Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
Java applications may throw the error “java.lang.OutOfMemoryError: Failed to create a thread: retVal” when they cannot create a thread.
Join the DZone community and get the full member experience.
Join For FreeUnderstanding the Error
The OutOfMemoryError thrown appears not because of insufficient heap memory but due to the following reasons:
Excessive Thread Creation
If the application creates too many threads, it may reach a limit where no more threads can be created. In such cases, it is crucial to investigate the stack trace thrown with the error and reduce the number of thread creations at the application code level.
Insufficient Native Memory
This issue arises when there is not enough native memory available for the Java process to create a thread. The operating system might have inadequate memory, which can lead to this error.
System Resource Constraints
There could be multiple processes running, and it might have reached the maximum threads allowed to be created on the machine on which the application is running.
Java Options to Resolve the Issue
Below are the ways in Java through which the memory used by the Java process can be reduced to give more space for native memory to create a thread:
- Reduce the heap size. Reducing the maximum heap size will reduce OS memory usage and allow the process to use it for native operations, such as creating threads. Also, setting a low memory value for the minimum heap size instead of setting equal values for min and max heap size will reduce the committed space in memory for the heap, making it available for the process to use for native space.
- Reduce the thread stack size using the XSS parameter. This will reduce the memory allocated to each thread, which in turn would allow the creation of new threads.s
- Reduce the metaspace/any other memory space available, which would allow more native memory to the process.
The above setting needs to be changed carefully without leading to another issue in these areas.
Linux Options to Resolve the Issue
Inspecting Existing Processes
On the Linux machine where the OOM error in a new thread occurs, check the various processes and terminate any unnecessary ones. Reducing the number of processes can help reduce the overall thread count on the machine.
Adjusting threads-max
The Linux kernel parameter threads-max sets the maximum number of threads per process. The current value can be viewed using the command below:
cat /proc/sys/kernel/threads-max
Increasing the value of this parameter can allow the application process to create more threads.
Adjusting ulimit -u
Running the command below can help to check the maximum processes supported for the user on the Linux machine:
ulimit -u
Increasing this value to a higher value also helps indirectly create more threads in the system. This value can be increased to a really high value or unlimited to help create more threads in the system.
Managing High Memory Usage
Linux systems attempt to load all pages into available memory and cache them until free memory is low. This behavior ensures that needed pages are loaded quickly from the cache in RAM rather than from disk/swap, which will be slower.
This behavior causes the free memory to go down. It can be confirmed by running the below command, which will show the available free memory and how much swap is being used:
free- h
If available memory is low and swap memory is not used, it indicates that cached pages are not being cleaned up from RAM. It can be freed up by manually or automatically clearing caches.
Manual Cache Clearing
The following commands can be run to clean up the caches in RAM so that the free memory can be increased:
echo 1 > /proc/sys/vm/drop_caches
This command clears the page cache.
echo 2 > /proc/sys/vm/drop_caches
This command clears directory entries and file data.
echo 3 > /proc/sys/vm/drop_caches
This command clears the page cache, directory entries, and file data.
Monitor free memory after running these commands and periodically repeat them if free memory decreases again.
Automatic Cache Clearing
If the RAM memory is not getting cleared, then there are a few parameters that can be used to clear the RAM memory. Below is one of the first parameters to try:
“/proc/sys/vm/swappiness”
The above parameter controls how the kernel moves the processes out of RAM to the swap area on the disk. By default, this value is 60 in SUSE Linux. Increasing it can help clear RAM and move contents to swap, while decreasing it has the opposite effect. By monitoring the memory and swap usage, this parameter can be fine-tuned to strike the right balance. Moving things out of RAM memory will create things to move to swap, and loading the contents later from swap will increase the response time.
Conclusion
The OutOfMemoryError thrown while creating threads can be resolved using the techniques mentioned in this blog. Both adjusting Java parameters and Linux parameters, depending on the scenario, will help eliminate this OutOfMemoryError.
Opinions expressed by DZone contributors are their own.
Comments