Thursday, April 25, 2024
HomeJavaSimulating & troubleshooting StackOverflowError in Scala - Java Code Geeks

Simulating & troubleshooting StackOverflowError in Scala – Java Code Geeks


On this sequence of simulating and troubleshooting efficiency issues in Scala, let’s focus on find out how to simulate StackOverflowError. StackOverflowError is a runtime error, which is thrown, when a thread’s stack measurement exceeds its allotted reminiscence restrict. 

Pattern Program

Here’s a pattern Scala program, which generates the StackOverflowError:

package deal com.yc
class StackOverflowApp {
}
object StackOverflowApp {
   def important(args: Array[String]): Unit = {
       System.out.println("hit enter to begin the StackOverflowApp")
       System.in.learn()
       begin()
   }
   def begin(): Unit = {
     one other()
   }
   def one other(): Unit = {
     begin()
   }
}

You’ll be able to discover the pattern program accommodates the ‘StackOverflowApp’ class. This program does the next:

  1. ‘important()’ technique of this class invokes ‘begin()’ technique 
  2. ‘begin()’ technique invokes ‘one other()’ technique
  3. ‘one other()’ technique invokes ‘begin()’ technique as soon as once more 

Thus ‘begin()’ and ‘one other()’ strategies (i.e. #b and #c) name one another recursively for an infinite variety of occasions. 

As per the implementation, the begin() technique and the one other() technique will likely be added to the thread’s stack body an infinite variety of occasions. Thus, after a number of thousand iterations, the thread’s stack measurement restrict could be exceeded. As soon as the stack measurement restrict is exceeded it’ll lead to ‘StackOverflowError’.

Execution

Once we executed above program, as anticipated ‘java.lang.StackOverflowError’ was thrown in seconds:

java.lang.StackOverflowError
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        at com.yc.StackOverflowApp$.begin(StackOverflowApp.scala:15)
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        at com.yc.StackOverflowApp$.begin(StackOverflowApp.scala:15)
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        at com.yc.StackOverflowApp$.begin(StackOverflowApp.scala:15)
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        at com.yc.StackOverflowApp$.begin(StackOverflowApp.scala:15)
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        at com.yc.StackOverflowApp$.begin(StackOverflowApp.scala:15)
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        at com.yc.StackOverflowApp$.begin(StackOverflowApp.scala:15)
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        at com.yc.StackOverflowApp$.begin(StackOverflowApp.scala:15)
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        at com.yc.StackOverflowApp$.begin(StackOverflowApp.scala:15)
        at com.yc.StackOverflowApp$.one other(StackOverflowApp.scala:19)
        :
        :

The best way to diagnose ‘java.lang.StackOverflowError’?

You’ll be able to diagnose StackOverflowError both by a guide or automated method. 

Handbook method

When an utility experiences ‘StackOverflowError’ it will likely be both printed within the utility log file or in a normal error stream. From the stacktrace it is possible for you to to determine which line of code inflicting the infinite looping.

Automated method

Then again, you may also use yCrash open supply script, which might seize 360-degree knowledge (GC log, 3 snapshots of thread dump, heap dump, netstat, iostat, vmstat, high, high -H,…) out of your utility stack inside a minute and generate a bundle zip file. You’ll be able to then both manually analyze these artifacts or add it to yCrash server for automated evaluation. 

We used the Automated method. As soon as the captured artifacts had been uploaded to the yCrash server, it immediately generated the beneath root trigger evaluation report highlighting the supply of the issue. 

Fig: yCrash highlighting thread might lead to StackOverflowError
Fig: yCrash exhibiting the stack hint of the thread inflicting StackOverFlowError

You’ll be able to discover the yCrash software exactly declaring the thread stack size is bigger than 400 traces and it has potential to generate StackOverflowError. The Instrument additionally factors out the stack hint of the thread which is happening an infinite loop. 

Utilizing this info from the report, one can simply go forward and repair the StackOverflowError.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments