Jstack is a built-in tool of JVM which is used to analyze the performance of java program.It prints the stack information of a process to the standard output of your system.Of cause you can redirect the output of the jstack to a file for later analysis.When you want to find the bottleneck of a java program,you can use jstack.For example: first,run you program named "myprogram" on the server and then figure out the process id (pid:2133) of your program(you can use "ps" command in linux shell). the last,start the jstack tool during the running of your program like this:
jstack 2133 > out.txt
then you can analyse the content of "out.txt" to find out the bottlenect of your program.To make the result more accurate,you need to take samples randomly of the program process stack,This is rather simple,right?
But how can the content of the output file of the jstack show you the bottleneck ? To understand this you need to know what do the jstack have done when it works.
When the jstack works,it reads the current stack information of the process,and that's it,that's all the jstack do.The stack information will show you which functions are the process invoking at the point you run jstack and which function is invoked by which.Find out the most frequently invoked functions(there may be serveral of them) in your program from the stack sample files you take. These "Most Frequently Invoked Functions" is right the bottleneck of your program.
why is the "Most Frequently Invoked Functions" the bottleneck of the program? Now let's make an explaination. The jstack caputre the stack information at one point during the running of the program, if some funcions' call information frequently appears in the jstack output sample file,that's indicating that the functions execution may take a long relatively long time,which is the reason why their stack information being easily caputured by jstack. So we can come to a conclusion:the most frequently appeared functions in the jstack output information are the bottlenecks of the program.
Do you understand now ?