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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  1. DZone
  2. Coding
  3. Languages
  4. Simulating and Troubleshooting StackOverflowError in Kotlin

Simulating and Troubleshooting StackOverflowError in Kotlin

In this series of simulating and troubleshooting performance problems in Kotlin, let’s discuss how to simulate StackOverflow errors.

Ram Lakshmanan user avatar by
Ram Lakshmanan
CORE ·
Jan. 17, 23 · Tutorial
Like (1)
Save
Tweet
Share
2.08K Views

Join the DZone community and get the full member experience.

Join For Free

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


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


Sample Program

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

 
package com.buggyapp  
    class StackOverflowApp {      
         fun start() {         
             start()      
         }   
     }    
   fun main() {      
     System.`in`.read()         
        try {            
           println(StackOverflowApp().start())         
           } finally {         
          System.`in`.read()         
          }   
   }


You can notice the sample program contains the StackOverflowApp class. This class has a start() method, which calls itself recursively. As a result of this implementation, the start() method will be invoked infinitely.

start() method repeatedly added to thread’s stack, resulting in StackOverflowError

Fig: start() method repeatedly added to the thread’s stack, resulting in StackOverflowError

As per the implementation, the start() method will be added to the thread’s stack frame an infinite number of times. Thus, after a few thousand iterations 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:

 
Exception in thread "main" java.lang.StackOverflowError 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
at com.buggyapp.StackOverflowApp.start(StackOverflowApp.kt:5) 
:
:


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 stack trace, 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,…) from your application stack within a minute and generate a bundle zip file. You can then either manually analyze these artifacts or upload them to the 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. 

   Fig: yCrash highlighting thread may result in StackOverflowError

                             Fig: yCrash highlighting thread may result in 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 exact 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.

Implementation Open source Kotlin (programming language) Error message Use case

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Implementing Adaptive Concurrency Limits
  • 9 Ways You Can Improve Security Posture
  • The Real Democratization of AI, and Why It Has to Be Closely Monitored
  • PostgreSQL: Bulk Loading Data With Node.js and Sequelize

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: