Tuesday, March 5, 2019

Understanding Strings in Java


Hello Friends, Welcome to my blog. A Blog which shares the deep secrets inside the java and presents it in front of you so you can understand the concept easily and can accommodate it in your professional life.

Introduction
Today's topic is the strings in java. Strings are the immutable objects in java. Once it is created it cannot be changed. if one needs to change the string, java creates new strings for you. At the core, java manages a pool of Strings. Whenever anyone requests to store the string, java first checks the existence of that string in the pool, if it is there simply it returns the reference object of that string to the caller.

One might ask why is so! Java is using the concept of "String interning". It is a method of storing only one copy of each distinct string. this will allow the runtime to save memory. String interning is an example of "Flyweight pattern" software design pattern.

I hope you are a clear idea now about the strings in java so let's get our hand dirty with programming and take our application to the operation theater and scissors it with the heap dump. If you are a newbie to the heap dump refer my blog here. I have added a breakpoint at the last sentence and in this way I have captured the heap dump of my application.

Let's take below example.


In the above application, We have created 6 strings each of them store the "abcd" value in different ways. some store directly, while others are doing the concatenation. But as the string interning method, only one instance of "abcd" is stored. Let's discuss each step in detail


  • on the line #9 first string is initialized with string "abcd, As of now string pool doesn't consist this string so java initially put this string in the pool & return the reference to the user.
  • on the line #11 third string is initialized with the concatenation of "ab" and "cd". Here java stores the "ab" and "cd" string in the string pool, concat them and found that result already exist in the pool. so instade of creating new string, it return the referece to it. so now the first variable and third variable are pointing to the same value with the same reference variable.
  • on the line #12 same is happened, all the 4 string stored in the string pool. java does concatenation on them and found that the result is already there in the pool. it returns the reference to the value.
  • on line #13 same is happened as above. till now first, third, fourth and fifth variables are pointing to the same value with the same reference variable.
  • on line #10 and #14, we have used a new operator. but still, the principle will not change for the string interning. here the second and sixth variable will point to the same value but having distinct reference variable.

Above steps can be understood by below screenshot


Here is the output of the program

I Hope you have a clear understanding of the comparison operator and the equals method. if not here is the few words can help you. comparison operator "==" when used on the objects (not on primitive) it compares the reference variable; do they are same or not. here firstString, thirdString and fourthString are having same reference variable. but the firstString, secoendString, and sixth string are having different reference variable (although they point to the same value, the variable will refer to different pointers stored on a different location) . as opposite the equals method will compare the actual content present in the variable.

Below are the few footprints when we open the heap dump of the application. if you see below image, you will come to know that first, third, fourth and fifth variables are having same reference #1662. however, the second variable is having reference #1663 and the sixth variable is having reference #1664. but in the end, all of them are having a value "abcd" which is located on location #1683.

Refer below screenshot for heap dump instance view.


Each of the variable after expanding their properties







So that's all friend hope you have understood the immutable nature of the string and how java store the string. if you have any question please comment below. your feedback is also valuable to improve my future blogs. do share it!

Happy Coding & Have a wonderful day.
-Anil Sable

Saturday, March 2, 2019

How to take java thread dump on windows, Linux & Other Operating System


Hello Friends, Today in this blog we will be learning how to take a thread dump on Windows and Linux Operating System. We can take a Thread dump of a local running process or remotely running process. If you already have a thread dump and just looking for the information to analyze then you can refer my blog here

Let's get started then.


Capturing Thread dump of Local Running Process on Linux Operating System
We will be using kill command here, This is the most popular command in Linux to send a different kind of signals to the process. this command is only available on Linux and not on windows. We have to send the SIGQUIT signal and the process id with this command. To learn different Linux kill command signals check out here. refer below steps.

1. Identify the process id of the java application for which we want to take a thread dump. You can use the ps command to list all process & grep the java keyword.
Command: $ ps -ef | grep "java"
Example:

2. use the "Kill -3" command followed by process id which will take a thread dump of the process and put it to STDOUT of the application. Here -3 is equivalent to send SIGQUIT signal to the process.
Command: $ kill -3 <PID>
Example:


Capturing Thread dump of Local Running Process on Windows or Other Operating System
We will be using jstack & jps command here. this command ships with your JDK installation bundle and present in the bin directory of the JDK. You can use this command on Linux as well as Windows & Other Operating system. before running these commands ensure JDK bin directory is added to the PATH environment variable. below are the steps

1. Identify the java process id for which you want to take a thread dump. for this, you can use jps command. Executing this command will list the all running java application. You need process id from the output list.



2. Now we know the process id, use the jstack command to print the thread dump on the console.



3. You can redirect output to separate text file which will be easy to maintain & process by thread dump analyzer tools.




Capturing a thread dump of remotely running java process
In this technique, we don't have to ssh or log in to the remote system. We have to enable the JMX Monitoring on our java application before running. When we enable this functionality, java application opens a port over which many things can be carried out. like statics of the application, thread dump, heap dump. You can read more about this here




Using GUI Tools for Thread dump and other monitoring tasks
JDK provides 2 GUI tools for application monitoring. both ship with JDK bundle & present in the bin directory of the JDK. "JConsole" is one tool from you can see the live thread stack trace. you can use this tool on Linux as well as on Windows for the locally running or remotely running java process. Below are the few features provided by this tool
  • View Memory Status
  • View MBeans
  • View Stack trace of a thread
  • VM Status

For remotely running process you need to enable JMX Port on the process.



 JConsole Tool

There is another tool called "JVisualVM", this tool will give you more insights than "JConsole". Few features of this tool are listed below.

  • Take Thread Dump
  • Take Heap Dump
  • Check Heap memory status
  • View stack trace of a thread
  • Ask JVM to invoke GC
  • Check CPU Usage
  • Analyze Heap dump
  • Sampler 
  • Profiling 






That's all for this tutorial friends. Please let me know your comments, questions or suggestion in below comment box, I will definitely try to improve the content and resolve your queries.

Thursday, February 21, 2019

Find out latency increase issue while performance test, take java thread dump and analyze it


Hello Friends in this blog we will be learning how to analyze thread saturation. if your application facing latency issue on load/performance test you should read this article.   

Background of the Problem Statement 
In a large complex java application, it happens many times that few operations run well when running in isolation. But the Latency of the calls increases during performance/load test. This might be happening due to the poor design of the multithreaded system. Below are the few signs which might cause slowness/increase latency to your application calls & this may vary depending on the system. am listing out the generic problem which everyone might encounter.
  • Excessive use of synchronization with improper lock objects
  • Connect to an external system, where the external system does not guarantee immediate response
  • Executing expensive SQL Queries / Using non-indexed columns on the OLTP system
  • Configuring the low number of thread pool & request incoming rate is too high, we can say as thread tuning is not well


How Multithreaded system works
Before we understand the problem in depth, let us understand how multi-threaded system work. Let us take an example of apache tomcat web server. each application server has a thread pool. it has a finite number of threads are defined like 50 or 80 or 100. whenever a request comes into the system, a corresponding job is added to the queue. job scheduler/thread manager check for free thread & assign the job to the thread for execution. and in this way, the job gets on to his journey of serving your request. Here below image depict the picture of what we discussed just now

Ultimately now the job for fetching employee details of id 1234, will assign to thread "Thread #1" as it is an ideal state & ready to work. remaining threads are currently working on another task. so they cannot take other tasks they get freed.

Let's get back to our problem statement
So now you have understood, how the multithreaded system works, you can imagine what can happen if all threads are currently working/running state & constantly new jobs are coming into the system. what will happen in this case? All new job will be queued up. as in when thread frees from their existing task, it will pick up a job from the waiting queue. this will impact the execution time of the request. so now the caller will have to wait till the job was in queue + actual job execution time. in this way, latency gets increased. So now we have understood that queuing up jobs will cause latency increase. Thread execution time should be finitely based on what type of system you are designing & what level of TPS you want to archive from your system. let us see some scenario that one should avoid, which cause the long-running operation execution on the thread. 

So now you have aware of what to do and what not. But how will you identify the problem the in the large complex application if you face increasing latency, slowness problem under load or performance test? 

Just to simulate the latency, I have developed the sample spring boot application which we will be used to identify the problem. I have developed this application just to simulate the thread saturation. It is having "thread.sleep()" statement to simulate latency by external calls or heavy database query.




We run the demo application, & hit the load test using SOAP UI tool. In below screenshot, you can see the which operation is in progress & how many time it is taking.


Here is the statics of SOAP UI Load test. You can see the latency of the request min, max, and average.



So at this point, we are ready to take the thread dump. Our performance test is already running & in SOAP UI test we have already seen that our threads are getting saturated & latency is increasing for the REST calls. I will show how we can take thread dump on Windows & Linux operating systems. 

How to take a thread dump and analyze it 

1. For taking a thread dump of the java process, JDK has provided few commands in the bin directory. make sure you have updated the PATH variable to include java bin directory. otherwise you are in the bin directory of JDK.


2. For taking thread dump, you should know for which java process you want to take a thread dump. JDK has provided "jps" command to list the java process. below is the sample execution of the command. am executing command -v option which will give me more verbose details about the process. and anyone can distinguish between the different Java process running simultaneously under one JVM. If you are on Linux you can use ps -ef | grep "java" to list all java process. in below screenshot, you can see sample java application is running with process id PID 31684.



3. Now we have found the java process for which we have to take the thread dump. You can use "jstack" command with process id and redirect the output to the file. if you are the linux user you can use kill -3 <<PID>> command to send the signal to java process which will dump the thread dump on stdout.



4. Now you have a thread dump file. a Thread dump file is a simple text file in which state of each thread is dumped with its call stack. there is various information associated with it like thread name, is it a demon thread, thread priority in JVM, thread priority in os, os transaction id, thread state, monitor. below is the screenshot of the thread dump file.




5. Now you have a thread dump file. there are many free tools which can analyze your thread dump and tell you what's wrong with your application. you can google for thread dump analyzer you will get a bunch of results which can analyze your thread dump. One of my favorites is fastthread.io. This website will give you the graphical representation of your thread dump. Let's analyze the result.



6. Open up the website and upload your dump file there. hit the analyze button. You can upload multiple thread dump file here.


7. Once processing is complete. you will see the report in the format of graphs and text. below is the sample report. Here in the below report, it is mention that analyzer has found the problem. 


Below 9 threads were blocked. you will get a nice description of the problematic scenario


Above image indicate that thread-8 has obtained a lock and did not release and due to this other threads are waiting. you can click on each thread to check in the call stack at what point they are blocked.





In this way, we can analyze the thread dump and can get rid of the problematic coding path.

Sunday, February 3, 2019

Analyzing Memory Leak, Heap Dump of java application using Eclipse Memory Analyzer Plugin and Visual VM


Hello Friends in this tutorial we will be learning how to analyze if your Java application has a memory leak. if your java application is crashing or not responding and you are getting an intermittent error of java.lang.OutOfMemory then you must refer to this tutorial.

Outline of this Tutorial
  • Quick Information About memory leak 
  • Start with checkout GitHub code which has a memory leak
  • Configure JMX Monitoring & Setting Maximum Heapsize
  • Hitting few calls till we get the OOM Response
  • Capture & Inspect Heap Dump to Check whats Wrong in the code flow

Let's Understand what is a memory leak 
First of all, let's understand what is a memory leak in an application. All application uses the memory to store its object. Let say I have an Inventory Management System, then before updating the data to the database, the application needs to input it from user & save on the memory & further code flow will take the value and update it to the database. suppose in this flow the object am saving on memory does not get clean and will exist there for a long running time. although this object is not useful still it is consuming my memory. such leaking of an object may not take much memory in a day but over a month will consume a lot of space in memory. in such a scenario, the application will face the problem if it is reached to maximum memory capacity. for storing new object or performing its routine task it will not get empty space & it started crashing. Such scenario java application throws OutOfMemory error.

JVM has an internal routine to clean up the unused objects from heap/memory, but if the leaking object is tied to a live reference then java will not clean those object for you. it will store those object and once you get out of the maximum memory capacity you will get java.lang.OutOfMemoryError.


What our demo Application will do
We have developed a demo application by which we will reproduce the OutOfMemory scenario. checkout GitHub application from here. In this application, we will be uploading the image file from the user from XYZ processing. During we are storing the image in ArrayList and we forgot to remove the image from ArrayList once our processing is complete. This ArrayList will be stored in one singleton object.





Configure JMX Monitoring & Maximum heap size for our java application
Here we have configured parameter to our java application which will help us to monitor our java application. You can read more for JMX at below link

https://docs.oracle.com/javase/6/docs/technotes/guides/management/agent.html

Below is the build.gradle file for the demo application.

bootRun {
 jvmArgs = [
  
 // configure heap size minimum 64mb and maximum 256mb
 "-Xms64m",
 "-Xmx256m",
 
 // enable remote debugging
     "-Xdebug",
     "-Xrunjdwp:transport=dt_socket,server=y,address=8001,suspend=n",
     
     // enable JMX remote monitoring
     // https://docs.oracle.com/javase/6/docs/technotes/guides/management/agent.html 
     "-Dcom.sun.management.jmxremote",
  "-Dcom.sun.management.jmxremote.port=9010",
  "-Dcom.sun.management.jmxremote.local.only=false",
  "-Dcom.sun.management.jmxremote.authenticate=false",
  "-Dcom.sun.management.jmxremote.ssl=false"
    ]
}

Here we started JMX service on "port" 9010. we have put "local.only" setting to false which means we can connect our monitoring tool to this application from a remote host as well. "authentication" is false, will be good for the development environment. we have added some JVM argument about the heap size, we have set minimum heap size to 64 MB and maximum size to 256 MB. This will help us to reproduce this issue quickly.


Sending Request to Application
We will be using CURL to send the request to our application. You can use Postman, ARC tools to send the request. You can also use JMeter, Dynatrace tools to run the load test. I have uploaded the 8MB File much time through post request which will help me to get the scenario to reproduce very easily.

executing curl command for upload file


Once you reach to this step, you will see you are getting many OutOfMemoryError in logs. I have attached below screenshot for the logs.



Let's start our monitoring tool to do analysis what's wrong with the application
In the market, there are various tools to monitor the java application using JMX service. One application provided out of the box when you install the Java Development Kit. It is known as "jvisualvm". If you open the installation/bin directory of your JDK you will see jvisualvm.exe executable file. Just launch it & GUI will come in front of you. In the below image you can see highlighted executable file.


Once you launch the visualvm, you need to select the host which you wants to monitor. By default, all local running java application will be visible in the local window. refer below screenshot.

From Remote section, you can add a remote host, by providing the hostname & JMX port and authentication if any. our demo application is running on localhost machine it is shown therein Local section & it is highlighted. In Below Screenshot we have connected to our local application. Below are the step you can perform to do memory analysis using jvisualvm.


  • Connect to the application using jvisualvm. the application can be local or remote.
  • Click on Heap Dump & Open Heap dump window.

  • Switch to Classes Tab from Summary Tab
  • Sort the List by size column. This may ask to calculate the retained value, allow them.
  • Select the Item which is consuming Maximum of memory size and right click on it and select "Show in Instance View"

  • From Instance View Sort again instance by size. Choose the first element, Expand the tree present in Reference tab. From here you can get the idea how this element get stored in memory and not cleaned.

  • In the above screenshot, you can see on the reference tab, SomeSingletonInstance class is storing all the reference to these items. in this way you can check the remaining instance as well, which are consuming memory.


Using Eclipse Memory Analyzer Plugin to analysis

Using jvisualvm might be not so helpful if your objects are small in size. visualvm will not collect the similar type of object and provide you graphical representation. Eclipse head dumps analyzer plugin will scan your heap dump and provide you the graphical representation of your memory & source from where this data is created. follow below steps to do analysis using eclipse memory analyzer.



  • You need to install "Eclipse Memory Analyser" software from Eclipse Marketplace. To do so click on help -> Eclipse Marketplace and search for "Eclipse Memory Analyser"

  • Once You Install the Software inside your Eclipse it will ask for a restart to adopt the changes.
  • Click on New -> "Others" and in dialogue box under "Other" click on "Heap Dump"

  • If your process is local then this dialogue box will list all the running java process. Once you select the process, this tool will capture heap dump for you.

  • If your process is remote, you can take "Heap Dump" of the remote process using jvisualvm, jmap command. Open this Heap Dump file in Eclipse. 
  • Once the Heap Dump Project open, You will see the Graphical Representation of the Objects.

  • Click on "Dominator Tree". Sort the List by Percentage Descending Order.
  • Expand the Tree which is having the highest usage percentage. As you go to the root of this tree you will come to know from which class this memory leak is happing. You can correct the flow there. 



  • So above Highlighted Items are consuming memory and not releasing them although they are done with there task.


Summary & things to be taken care to avoid memory leak

  • The memory leak will happen if you are storing objects on the heap and not releasing them. if you don't need then clean them.
  • Possible cause of memory leak is poor design using Static classes & Singleton instances. Don't use collection classes in static scope or collection classes in singleton instance. If you do so Ensure that you are properly releasing the object. Have a 4 to 5-hour performance test and analyze the heap dump on each interval of 1 hour.
  • For taking heap dump we can use the jmap command. this will be there in the installation/bin directory of your JDK.
  • There are GUI Based tools also which will help you to take a heap dump. jvisualvm will be available in the JDK installation directory. You will also get many eclipse plugins to do so.
  • While Analysing Heap Dump calculates retained size & sort the instance based on size, group them in classes & go to the root of the objects whose size are big enough.
  • While going to root you will get an idea about the flows & classes by which this object was stored.

Ok, that's all for this tutorial friends. you have any question, suggestion please comment below. 
Happy Coding :)









Sunday, January 13, 2019

Using Graphics Magick with Java to Convert, Resize, Blur, Rotate and Perform Basic Image Processing operations




Hello, Friends today in this blog we will be learning how to use Graphics Magick to perform the basic operation on Images using Java.

What You will need


How the Graphics Magick & IM4Java works with each other

So before we get our hands dirty in coding lets first understand, How things work. Graphics Magick is Command line Utility which is used for image processing. We can perform many image processing operation like resize, shrink images, drawing shapes, annotate images, blurring images, rotating images & so on. It has rich sets of function by which one can develop a full-fledged image editing software like Photoshop. IM4Java is Java Wrapper build around Graphics Magick to take input from the user and convert the input to appropriate Graphics Magick commands.



Install Graphics Magick on your system

  • Download Graphics Magick from here & install on your system.
  • Make Sure that you have added the path of Graphics Magick installation directory to PATH environment variable as per your Operating System.
  • Open Terminal / Command prompt & Verify graphics magick is accessible by command "gm -version"  



Let's get started with coding, Here is our build.gradle file

apply plugin: 'java'
apply plugin: 'application'

dependencies {
    compile group: 'org.im4java', name: 'im4java', version: '1.4.0'
    testImplementation 'junit:junit:4.12'
}

mainClassName='org.javahotfix.gm.app.SimpleGraphicsOperation'

sourceCompatibility=1.8
targetCompatibility=1.8

repositories {
    jcenter()
}

  • Here we will be writing our code in SimpleGraphicsOperation class
  • We have added the dependency on im4java in build.gradle on line number 5.

Here is First Small Example to Resize Image to create its Thumbnail (SimpleGraphicsOperation.java)

public void resizeImage(String originalFile,
                 String targetFile,
                 int width,
                 int height) throws InterruptedException, IOException, IM4JavaException {
  ConvertCmd command = new ConvertCmd(true);
  IMOperation operation = new IMOperation();

  operation.addImage(originalFile);
  operation.resize(width, height);
  operation.addImage(targetFile);

  // Execute the Operation
  command.run(operation);
}


In the above example, we have created an object of ConvertCmd having a boolean constructor with value true. This will tells IM4Java library to use Graphics Magick. By Default IM4Java uses Image Magick for performing its operation.

IMOperation class used to generate equivalent graphics magick command for a various operation like resize, blur, draw border etc. check out more operation here.


Working with Streams (SimpleGraphicsOperation.java)
If you are working with stream-based application & don't have a physical file path, then you can use below example where we have used the stream-based approach.
public void resizeImage(
      InputStream input, 
      OutputStream output, 
      int width, 
      int height,
      String format) throws IOException, InterruptedException, IM4JavaException {
     
     ConvertCmd command = new ConvertCmd(true);
     
     Pipe pipeIn = new Pipe(input, null); 
     Pipe pipeOut = new Pipe(null, output);
     
     command.setInputProvider(pipeIn);
     command.setOutputConsumer(pipeOut);
     
     IMOperation operation = new IMOperation();
     operation.addImage("-");
     operation.resize(width, height);
     operation.addImage(format + ":-");
     
     command.run(operation);
 }

Adding Blur Effect to Image having border (SimpleGraphicsOperation.java)
public void performMultipleOperation(
      InputStream input, 
      OutputStream output) throws IOException, InterruptedException, IM4JavaException {
     ConvertCmd command = new ConvertCmd(true);
     Pipe pipeIn = new Pipe(input, null); 
     Pipe pipeOut = new Pipe(null, output);
     
     command.setInputProvider(pipeIn);
     command.setOutputConsumer(pipeOut);
     
     IMOperation operation = new IMOperation();
     operation.addImage("-");
     operation.blur(100.0);  // Radius of Blur effect
     operation.border(15,15); // width height parameter
     operation.addImage("-");
     
     command.run(operation);
}


Join multiple images horizontally (SimpleGraphicsOperation.java)
public void joinMultipleImages(
      InputStream input1,
      InputStream input2,
      InputStream input3,
      InputStream input4,
      String output) throws IOException, InterruptedException, IM4JavaException {
 
     BufferedImage img1 = ImageIO.read(input1);
     BufferedImage img2 = ImageIO.read(input2);
     BufferedImage img3 = ImageIO.read(input3);
     BufferedImage img4 = ImageIO.read(input4);
       
     ConvertCmd command = new ConvertCmd(true); 
     IMOperation operation = new IMOperation();
     operation.addImage();  // placeholder for first image
     operation.addImage();  // placeholder for second image
     operation.addImage();  // placeholder for third image
     operation.addImage();  // placeholder for fourth image
     operation.appendHorizontally();  // perform operation
     operation.addImage();  // placeholder for output image
     command.run(operation, img1, img2, img3, img4, output); // provide parameter base on placeholder parameter index
}

Thats all for now guys. I hope you have got a basic understanding of how we can use Graphics Magick with java through IM4Java. Hope you will do some more experiments on this. Below are the List of References that have been mention in this blog.