Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simulating & troubleshooting StackOverflowError in Scala

  1. #1
    Member
    Join Date
    Nov 2017
    Location
    USA
    Posts
    136
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Simulating & troubleshooting StackOverflowError in Scala



    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.

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

    https://youtu.be/PxfWuBEBcW8

    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 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.sc  ala:15)
            at com.yc.StackOverflowApp$.another(StackOverflowApp.  scala:19)
            at com.yc.StackOverflowApp$.start(StackOverflowApp.sc  ala:15)
            at com.yc.StackOverflowApp$.another(StackOverflowApp.  scala:19)
            at com.yc.StackOverflowApp$.start(StackOverflowApp.sc  ala:15)
            at com.yc.StackOverflowApp$.another(StackOverflowApp.  scala:19)
            at com.yc.StackOverflowApp$.start(StackOverflowApp.sc  ala:15)
            at com.yc.StackOverflowApp$.another(StackOverflowApp.  scala:19)
            at com.yc.StackOverflowApp$.start(StackOverflowApp.sc  ala:15)
            at com.yc.StackOverflowApp$.another(StackOverflowApp.  scala:19)
            at com.yc.StackOverflowApp$.start(StackOverflowApp.sc  ala:15)
            at com.yc.StackOverflowApp$.another(StackOverflowApp.  scala:19)
            at com.yc.StackOverflowApp$.start(StackOverflowApp.sc  ala:15)
            at com.yc.StackOverflowApp$.another(StackOverflowApp.  scala:19)
            at com.yc.StackOverflowApp$.start(StackOverflowApp.sc  ala: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 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 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.



    Fig: yCrash highlighting thread may result in StackOverflowError


    Fig: yCrash showing the stack trace of the thread causing StackOverFlowError

    You can notice the yCrash tool precisely pointing out the thread stack length is greater than 400 lines and it has 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.

  2. #2
    Junior Member
    Join Date
    Nov 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simulating & troubleshooting StackOverflowError in Scala

    Hi,
    I am trying ton open your link but its not opening what's the issue? I looking to learn this for my website which is toysshopforkids.com

Similar Threads

  1. Simulating & troubleshooting OOMError in Kotlin
    By Ram Lakshmanan in forum Debugging Tutorials
    Replies: 0
    Last Post: November 10th, 2022, 07:28 AM
  2. Simulating & troubleshooting deadlock in Kotlin
    By Ram Lakshmanan in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: September 29th, 2022, 11:42 PM
  3. StackOverFlowError
    By LostInCode in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 30th, 2017, 07:22 AM
  4. Getting stackoverflowerror
    By sravya in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 23rd, 2013, 04:02 AM
  5. Where to start? Simulating with Java
    By Teddy R in forum Java Theory & Questions
    Replies: 4
    Last Post: July 23rd, 2012, 01:20 PM