Thanks to visit codestin.com
Credit goes to dzone.com

DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Java and Low Latency
  • Fixing OutOfMemoryErrors in Java Applications
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  • Understanding Java Signals
  • Java’s Next Act: Native Speed for a Cloud-Native World
  1. DZone
  2. Coding
  3. Java
  4. Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error

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.

By 
Harish Kumar Murugesan user avatar
Harish Kumar Murugesan
·
Apr. 25, 25 · Analysis
Likes (4)
Comment
Save
Tweet
Share
4.7K Views

Join the DZone community and get the full member experience.

Join For Free

Understanding 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:

Plain Text
 
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:

Plain Text
 
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:

Plain Text
 
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:

Plain Text
 
echo 1 > /proc/sys/vm/drop_caches


This command clears the page cache.

Plain Text
 
echo 2 > /proc/sys/vm/drop_caches


This command clears directory entries and file data.

Plain Text
 
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:

Plain Text
 
“/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.

Java (programming language) Linux (operating system) Memory (storage engine)

Opinions expressed by DZone contributors are their own.

Related

  • Java and Low Latency
  • Fixing OutOfMemoryErrors in Java Applications
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers
  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: