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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Java Virtual Threads and Scaling
  • Virtual Threads: A Game-Changer for Concurrency
  • Deep Dive Into Java Executor Framework
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech

Trending

  • Implementing Explainable AI in CRM Using Stream Processing
  • Optimizing Serverless Computing with AWS Lambda Layers and CloudFormation
  • Useful System Table Queries in Relational Databases
  • Bridging UI, DevOps, and AI: A Full-Stack Engineer’s Approach to Resilient Systems
  1. DZone
  2. Coding
  3. Languages
  4. Simulating and Troubleshooting Thread Leak in Scala

Simulating and Troubleshooting Thread Leak in Scala

In this series of simulating and troubleshooting performance problems in Scala, let’s discuss how to simulate thread leaks.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
Apr. 12, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.0K Views

Join the DZone community and get the full member experience.

Join For Free

In this series of simulating and troubleshooting performance problems in Scala, let’s discuss how to simulate thread leaks. java.lang.OutOfMemoryError: unable to create new native thread will be thrown when more threads are created than the memory capacity of the device. When this error is thrown, it will disrupt the application’s availability.


Video: To see the visual walk-through of this post, click below:


Scala Sample Thread Leak Program

Here is a sample Scala program, which will generate java.lang.OutOfMemoryError: unable to create new native thread

Scala
 
package com.yc 
import java.lang.Thread.sleep 
class ThreadLeakApp { 
}
object ThreadLeakApp {      
      def main(args: Array[String]): Unit = {      
      System.out.println("ThreadApp started")         
         while (true) {            
           new ForeverThread().start()         
        }      
     }     
  class ForeverThread extends Thread {         
    override def run(): Unit = {             
      while (true) {               
        sleep(100)           
      }         
    }      
  } 
}


You can notice that the sample program contains the ThreadLeakApp class. This class has a start() method. In this method, ForeverThread is created an infinite number of times because of the while (true) loop.

 In the ForeverThread class there is the run() method. In this method, thread is put to continuous sleep, i.e., thread is repeatedly sleeping for 100 milliseconds again and again. This will keep the ForeverThread alive always without doing any activity. A thread will die only if it exits the run() method. In this sample program run() method will never exit because of the never-ending sleep.

Since the ThreadLeakApp class keeps creating ForeverThread infinitely and it never terminates, very soon several thousands of ‘ForeverThread’ will be created. It will saturate memory capacity, ultimately resulting in java.lang.OutOfMemoryError: unable to create new native thread problem.

How To Diagnose java.lang.OutOfMemoryError: unable to create new native thread?

You can diagnose the OutOfMemoryError: unable to create new native thread problem either through a manual or automated approach. 

Manual Approach

In the manual approach, you need to capture thread dumps as the first step. A thread dump shows all the threads that are in memory and their code execution path. You can capture a thread dump using one of the 8 options mentioned here. But an important criterion is: You need to capture thread dump right when the problem is happening (which might be tricky to do). Once the thread dump is captured, you need to manually import the thread dump from your production servers to your local machine and analyze it using thread dump analysis tools like fastThread, and samurai.

Automated Approach

On the other hand, you can also use yCrash open source script, which would capture 360-degree data (GC log, 3 snapshots of thread dump, heap dump, netstat, iostat, vmstat, top, top -H,…) right when the problem surfaces in the application stack and analyze them instantly to generate root cause analysis report. 

We used the automated approach. Below is the root cause analysis report generated by the yCrash tool highlighting the source of the problem. 

Fig:  yCrash reporting 2,600+ are created and they can cause ‘OutOfMemoryError: unable to create new native thread’
Fig:  yCrash reporting the exact leaking threads

From the report, you can notice that yCrash points out that 2608 threads are in TIMED_WAITING state, and they have the potential to cause OutOfMemoryError: unable to create new native thread problem. Besides the thread count, the tool is also reporting the line of code, i.e., com.yc.ThreadLeakApp$ForeverThread.run(ThreadLeakApp.scala:31) in which all the 2608 threads are stuck. Equipped with this information, one can quickly go ahead and fix the java.lang.OutOfMemoryError: unable to create new native thread problem.

Scala (programming language) Error message Thread pool Java (programming language)

Published at DZone with permission of Ram Lakshmanan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java Virtual Threads and Scaling
  • Virtual Threads: A Game-Changer for Concurrency
  • Deep Dive Into Java Executor Framework
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech

Partner Resources

×

Comments
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!