Skip to main content

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control

Java flight recording(JFR) help us to analyse and find the root cause of any slowness in our program along with CPU usage , hot methods and garbage collection , profiling etc.

To visualise we need to feed .jfr file to JDK mission control present in JDK bin folder.

After successful compilation , we should run the program with below option which will generate the .jfr and feed to mission control.

  command :  java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=duration=200s,filename=flight.jfr -cp ./out/ path-and-class-name


Below are some example to understand how this JFR can be helpful.

1. Lets consider we have created a java program in which we have used LinkList to store the elements and in same program we are using contains method inside a for loop of 1 million , in this case each time this contains method will be called then 1 million records will be scanned each time which can slow our program.

   To overcome this problem we should have consider HashSet instead if linkedList as data are stored in sequential way in LinkedList and using contains method increase the comparing time while in Set its O(1).

This kind of problem will be difficult to analyse from developer eyes as while developing more focus given to business logic compare to data type selection. Here JFR comes to rescue and point at exact method line as hot methods which should be looked into

2. Suppose we have synchronised a method  or block used by multiple threads but as we know synchronised block can be accessible by only one thread at a time. JFR can visualise and give you proper graphical representation  of  waiting , blocking and running time of thread and can point out exact blocking method which is causing issue.

3. Lets consider we created a Hashmap as   HashMap<Integer, String> map=new HashMap(). now we are putting key as primitive int and value as a string and we have 1 million records to put. Do you realise any issue here .

  No let me tell you, every-time you are calling put method using int as key, internally java is doing autoboxing to change the type of primitive type to Integer type. This can be visualise and can be found when you will run your program with Java mission control and will be able to see the differences after changing the type of key.



Comments

Popular posts from this blog

SECURITY IN SPRING BOOT : Authentication and Authorisation in Spring boot Application

SECURITY IN SPRING BOOT : Authentication and Authorisation in Spring boot Application In any application security is the most important aspect without this no application can be considered as standard application as it will have full exposure to hackers. Now to secure our application there are 2 important concept  Authentication and Authorisation  which we should know .     Authentication stands for authentication of the client from your request is coming and to do so we will receive unique username and password for that to validate the authenticity of client. If it passes through our authentication process and identified as valid client then we will proceed and allow to access our URI and resources.   Authorisation comes after authentication in which we will allow to access our resources based on type of client if client is an Admin then he will have full access , if client is Premium user then he will have access to all premium resources if client is...

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; Str...

Merge sort Implementation

  Merge sort   : In merge sort we will divide the array from middle recursively  until we have only one element in sub-array. After that we will merge sub-array in ascending order.     Time complexity for merge sort will be O(nlogn) as we are splitting the array from middle so each time it's getting half so for that  it will be log(n) and while merging we are traversing the whole array so for that it will be O(n).      So after combining both  time complexity will be O(n)+O(logn)= O(nlogn)      space complexity will be O(n) as we need a extra same size array to hold the unsorted array. class MergeSort { public static void sort ( int [] array) { int [] aux = new int [array. length ] ; sort (array , aux , 0 , array. length - 1 ) ; } private static void sort ( int [] array , int [] aux , int lo , int hi) { if (hi <= lo) return; int mid = (lo + hi) / 2 ; sort (arra...