 Welcome back once again. So today in this video we will be talking about profiling Java applications. So in coming slides we are going to see what we mean by profiling an application. And in the same slides we will also talk about how a profiling will help an application developer to develop a better applications. So profiling is a part of a software development cycle wherein after proper requirement analysis design testing and after QA before the product is delivered sometimes application is profiled to make sure it performs as per the set criteria and the set requirement specifications. So let us see how to profile in a Java application. So what we have in this video as a learning outcome is to make sure that once the student goes through this video student will be able to profile a Java application using JFR. So what is JFR? A JFR is Java Flight Recorder it is an inbuilt profiling tool provided to us by Oracle. So in the coming process we will be seeing like how to make sure the JFR flight recorder is launched what we need to take care by launching the JFR recorder and is it free is it open source is it available to all we all will be discussing in the coming slides. So let us move ahead to have a small hands on in which we will be profiling a small Java application. So we will discuss what type of code we are also profiling okay fine. So what is an application profiling in general? So an application profiling is nothing but it is a form of a dynamic program analysis which usually measures what we do in profiling here it is very important to note that profiling is far more different from testing. profiling ensures that it functions as per what it is expected to function. But in a profiling it is all a dynamic process what we what a person who does profiling of an application is more interested is is he is more interested in knowing the total memory uses of that application the CPU uses of that application and the users of instructions like what type of instruction the user is using or what type of instruction the program is more spending time in executing. So these steps form very important process while profiling application because sometime as a developer I would be discouraged from using a deprecated instructions and if in case some in some cases I still tend to use them. But during profiling if I find the deprecated instructions are the one which are consuming more time in exact program execution what we do is we will try to find out an alternative for that. So profiling also lets me have a proper analysis of users of instruction and at the end it also lets me profile an actual time spent by a program in calling methods and how much time in each method is consuming and how much time does method consume after 5 iterations after 10 iterations. So I can do an complete analysis of method called during runtime process. So we all will see like how to explore the profiling feature provided to us by Oracle. So let us see our hands on. So in the coming part of the video what we will be doing is we will be doing a small hands on in which we will be profiling a Java application. So prerequisite to this video is to make sure that you know the fundamentals of Java language it's make sure that you have JDK 8 and Java flight recorder installed on your system and it would be always better if we have an eclipse ID of version 5.4.5 or higher because we will be writing our code in eclipse to save our time. So for our ease of use what I have done I have already I already have a video here where in what I am using is I have an eclipse ID where I already wrote a code. So for example we are going to profile the following code which right now it is appearing on your screen. So what this code is doing let us first analyze what this code is doing. The code is trying to sort an array of integers and we all know that arrays.sort method uses a pivotal dual pivotal quick sort algorithm. And as per the standard analysis of algorithms what we know is that quick sort performs worst when we ask it to sort a numbers which are already sorted. So it's a worst case for quick sort. So we are going to see like how is it is it really a case like we can monitor that quick sort is consuming more CPU cycles when we ask it to sort a random series of integers or a sorted series of integers. So let us see here. So I will quickly explain this program and then I will let you know how to enable a profiling while executing this program. So it's a stepwise process. So what this process is doing it is first asking user to hit. So I have a few hit key base stops in my program because I want to execute my program only when my flight recording system is live. So in order to make sure that my code exactly runs during the window flight recording I have enabled scan from a user. So if I hit it starts and at the end of this program what I find is here if I hit a key it will end. Now what I am doing is I will run flight recorder for a duration of say 60 or 120 seconds because I know that this program can work. So I am going to give an input which at max the program can sort it in the time frame of 60 to 120 seconds. So given that this program will be first compiling it and then while running this application we will enable Java flight recorder and once we enable Java flight recorder we will get a JFR file which ends with an extension .JFR and with the help of Java mission control and the flight recorder in the mission control window we will be exploring that and we will see what exactly are being profiled in my Java profilers flight recordings. Let us do that. So in order to execute this code I am moving to a command prompt wherein I will command I will type the compile this program. So I am in the exact directory where my main.java program is there. So I have compiled it. So now I need to run this. Now before I run this application you need to make sure this application will be profiled. So there is a special way to run this application it is not like directly Java execute main. So how it works is I have already kept my commands ready. So this is how you are going to execute this program. For example here my program name is main and my first argument is the length of array which my program will be sorting and in what mode this 20,000 numbers would be inserted in the array. So if you go back to your code if it is sorted it puts number and increasing order. So probably it should consume more CPU cycles when we analyze the flight recordings. So going back I will run this application and after running this application before you run this is a command which starts flight recording. So in order to make sure it runs here you have to always insert the process ID. So the process ID is obtained by executer command GPS. So on one command prompt we will execute this command and I will just stop the program and I will execute only when I have initiated this command. So you observe the sequence that would explain how I am recording the flight recording. So I will copy this command and let me execute it. Let me paste it here. So now I want this to execute yes. Now it is executing but I have not initiated the sort code as of now. It is just waiting for a key to run that arrays.sort code in that code. So now before I run this I need to make sure that here so I have opened another command prompt in the same and here I am going to enable my flight recording session. So you need to make sure that my class name is main note down the process ID, the process ID is 7316. So I will note down this 7316, 7316 I will note down this in the command and then I will execute that command and I have to wait for 7316 am I right 7316 fine. Now I will be running this command. So I will copy this and what I will do is instead of 120 seconds I will ask it to just record it for 50 seconds. So I am sure that my array.sort works in 50 seconds. So here in the window here the code is running and here I will hit this flight recording command. So let me see it has to paste it first I think I am an insert. So I need to paste that command yes. So it is running now. So while it is running I will go back to command prompt and I will start hit enter. So now it is running. And make sure you do not exit before the seconds or 50 seconds you should not exit before this else your flight recording file will be corrupted. So what I will do I will wait for 50 seconds. I think it is almost 50 seconds now. Now during this what I can do is I can start my java mission control. So I will type jmc it opens the java mission control window. So it opens that and it also generates a source file of your sorted recording. So let me make sure it allow access. So this is jmc. So this is mission control and here is your flight recording. So close this you will have a flight. So this is the main the process which I have recorded. So now if you expand this allow access it asks me to browse a recording file. What I can do is I can even open that recording file directly from here. So this is the sorted the code which I asked you to open. So now this is a code. Now you can see that at max it is in the 30 seconds here. So you can see it also took 50 seconds. So you can decrease the increase in this. So you can find that here is where it consume more. At the same time it also lets you consume more number of memory what has been observed how much memory it is capturing. You can also see like what were the methods which were most responsible. So we have thread, we have main loop and we have events responses and queues as well here. So this is how we generally profile in machine application. And while profiling you have to always make sure that you properly execute the commands. So what have you seen? We have seen a quick and a short way to profile a Java application and to profile a Java application we have used the inbuilt Java flight recording sessions. And as a further reading I would suggest you to go through the link where you will get the open source profilers in Java and explore it so that you can explore more number of profilers available out in market and these are always open source. So these are the references and hope this video helps you in profiling Java application. Thank you.