Saturday, October 6, 2018

StackOverFlowError: Causes & Solutions


StackOverFlowError is one of the common confronted JVM error. In this blog post lets learn inner mechanics of thread stacks, reasons that can trigger StackOverFlowError and potential solutions to address this error.

To gain deeper understanding in to StackOverFlowError, let's review this simple program:

<<start:code>>
public class SimpleExample {

      public static void main(String args[]) {
           
            a();
      }    

      public static void a() {

            int x = 0;
            b();
      }

      public static void b() {

            Car y = new Car();           
            c();
      }

      public static void c() {

            float z = 0f;
            System.out.println("Hello");
      }
}
<<end:code>>

This program is very simple with following execution code:

1.       main() method is invoked first
2.       main() method invokes a() method. Inside a() method integer variable ‘x’ is initialized to value 0.
3.       a() method in turn invokes b() method. Inside b() method Car object is constructed and assigned to variable ‘y’.
4.       b() method in turn invokes c() method. Inside c() method float variable ‘z’ is initialized to value 0.

Now let’s review what happens behind the scenes when above simple program is executed. Each thread in the application has its own stack. Each stack has multiple stack frames. Thread adds the methods it’s executing, primitive data types, object pointers, return values to its stack frame in the sequence order in which they are executed.




Fig 1: Thread's Stack frame.

Java - Cucumber Hello World example (Eclipse + Maven + JUnit)

In the previous article we have seen JBehave Hello World example. In this article we will see same Hello World example but with using Cucumber.

Like JBehave, Cucumber is also used for behavior-driven development (BDD) testing. To know more about Cucumber click here.

Below is the eclipse folder structure -


Wednesday, September 26, 2018

Benefits of call stack tree




Call Stack Tree provides 3 wonderful benefits:
  1. One simplified view
  2. Performance Optimization
  3. Accurate Smoke Test

Let’s discuss them in detail in this article.

1. One simplified view

Thread dumps are the snapshot of all threads running in the application at given moment. Thread dump will have hundreds/thousands of application threads. It would be hard to scroll through every single line of the stack trace in every single thread. Call Stack Tree consolidates all the threads stack trace into one single tree and gives you one single view. It makes the thread dumps navigation much simpler and easier. Below is the sample call stack tree generated by fastThread.io.


                                            Fig 1: Call stack Tree

You can keep drilling down to see code execution path. Fig 2 shows the drilled down version of a particular branch in the Call Stack Tree diagram.


                                            Fig 2: Drilled down Call Stack Tree

Call Stack Tree shows you the class name, method name, and line of the code that has been executed and the number of threads that have executed the line of code.

                                     Fig 3: A single element from Call Stack Tree

From the above element in the Call Stack Tree, you can identify that call() method in buggyCompanyCallable.java is executed by 9 threads.

Saturday, August 25, 2018

Simple spring integration with springboot

In this example we will see a simple file poller application using spring integration. It will poll for a file in a particular location at certain interval and if file is found it will move it to another location.

There are 3 core components of spring integration -

Messages - Messages are the objects sent from one component to another. In our case a file is sent from file poller to file handler.
Channels - Assume channels are the network (a pipe) by which messages are sent from one component to another.
Adapters - Adapters route the output from one channel to the input of another one.



Please see the codes below -



Wednesday, August 15, 2018

Read/Write large excel (xlsx) file

In this article you will see how to write a huge excel file.

Using SXSSFWorkbook you can write large excel file. It is a Streaming version of XSSFWorkbook implementing the "BigGridDemo" strategy. This allows to write very large files without running out of memory as only a configurable portion of the rows are kept in memory at any one time.

Note - I have tested it to write 10,48,576 rows which created 40 MB excel file. If you want to write more rows than this then you may opt to write CSV file.

You need to add below dependencies in your pom.xml