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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • Detection and Response Did Its Job. Now Someone Has to Actually Fix It.
  • Embabel vs LangGraph4j: Two Agentic Philosophies for Investment and Risk Analysis in BFSI
  • The Jobs Matrix for AI: Four Boxes Instead of 40 Tools
  • Running TypeSafe AI's Jev Inside Neo4j for Confidence-Aware Sentiment Decisions

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Simulating and Troubleshooting StackOverflowError in Scala

Simulating and Troubleshooting StackOverflowError in Scala

Discuss how to simulate StackOverflowError, a runtime error, which is thrown when a thread’s stack size exceeds its allocated memory limit.

By 
Ram Lakshmanan
DZone Core CORE ·
Nov. 28, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In this series of simulating and troubleshooting performance problems in Scala, let’s discuss how to simulate StackOverflowError. StackOverflowError is a runtime error, which is thrown when a thread’s stack size exceeds its allocated memory limit. 

Sample Program

Here is a sample Scala program, which generates the StackOverflowError:

package com.yc 
class StackOverflowApp { 
}
object StackOverflowApp {   
   def main(args: Array[String]): Unit = {        
       System.out.println("hit enter to start the StackOverflowApp")        
       System.in.read()        
       start()   
   }    
   def start(): Unit = {     
     another()   
   }    
   def another(): Unit = {     
     start()    
   } 
}


You can notice the sample program contains the StackOverflowApp class. This program does the following:

  1. main() method of this class invokes start() method 
  2. start() method invokes another() method
  3. another() method invokes start() method once again 

Thus, start() and another() methods (i.e., #b and #c) call each other recursively for an infinite number of times. 

As per the implementation, the start() method and the another() method will be added to the thread’s stack frame an infinite number of times. Thus, after a few thousand iterations, the thread’s stack size limit would be exceeded. Once the stack size limit is exceeded it will result in StackOverflowError.

Execution

When we executed the above program, as expected, java.lang.StackOverflowError was thrown in seconds:

java.lang.StackOverflowError         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       at com.yc.StackOverflowApp$.start(StackOverflowApp.scala:15)         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       at com.yc.StackOverflowApp$.start(StackOverflowApp.scala:15)         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       at com.yc.StackOverflowApp$.start(StackOverflowApp.scala:15)         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       at com.yc.StackOverflowApp$.start(StackOverflowApp.scala:15)         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       at com.yc.StackOverflowApp$.start(StackOverflowApp.scala:15)         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       at com.yc.StackOverflowApp$.start(StackOverflowApp.scala:15)         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       at com.yc.StackOverflowApp$.start(StackOverflowApp.scala:15)         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       at com.yc.StackOverflowApp$.start(StackOverflowApp.scala:15)         
       at com.yc.StackOverflowApp$.another(StackOverflowApp.scala:19)         
       :         
       :


How To Diagnose java.lang.StackOverflowError

You can diagnose StackOverflowError either through a manual or automated approach. 

Manual Approach

When an application experiences StackOverflowError, it will be either printed in the application log file or in a standard error stream. From the stacktrace, you will be able to figure out which line of code causing the infinite looping.

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, etc.) from your application stack within a minute and generate a bundle zip file. You can then either manually analyze these artifacts or upload it to yCrash server for automated analysis. 

We used the automated approach. Once the captured artifacts were uploaded to the yCrash server, it instantly generated the below root cause analysis report highlighting the source of the problem. 

yCrash highlighting thread may result in StackOverflowError

yCrash highlighting thread may result in StackOverflowError

yCrash showing the stack trace of the thread causing StackOverFlowError

yCrash showing the stack trace of the thread causing StackOverFlowError

You can notice the yCrash tool precisely points out that the thread stack length is greater than 400 lines and it has the potential to generate StackOverflowError. The tool also points out the stack trace of the thread which is going on an infinite loop. 

Using this information from the report, one can easily go ahead and fix the StackOverflowError.

Video 

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


Scala (programming language)

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

Opinions expressed by DZone contributors are their own.

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook