Google

May 8, 2012

Java performance questions and answers: overview and profiling cpu

Q. When somebody says an application is running "very slow, what does he/she mean?
A. He/she is generally referring to one of two performance attributes –  latency or scalability. Latency describes how long it takes for a given task to complete, whereas scalability describes how a program's performance gets impacted under increasing load. Building high performance applications require 

  • Low latency –  for example,  low page loading times.
  • High scalability – for example, serving increasing number of users without adversely impacting performance. 
  • High availability – for example, staying up 24x7, without going down due to memory leak or running out of connections to the database or LDAP server.

As described in the above diagram, performance issues can occur due to a variety of reasons.  It can vary from a bad application, database, or infrastructure design that does not scale well to poorly tuned load balancer, virtual machine, application server, and from badly constructed SQL statements (e.g. a Cartesian join) and frequently back tracking regular expressions to thread contention issues caused by multiple threads simultaneously waiting for a long running locked method, block of code, or  database records. Memory and non-memory (e.g. sockets, connections, etc) leaks can also degrade performance by depriving your application of these scarce resources by either consuming more CPU cycles due to over working the garbage collection or by running out of memory or connections. 


Q. What tools do you need to profile a Java application?
A. For troubleshooting Java applications, there are basic tools like vmstat, hprof, JConsole, JAMon, PerfAnal, etc to more feature packed profiling tools like VisualVM, Netbeans profiler, eclipse TPTP (i.e. Test & Performance Tools Platform), etc to tools that can be used in production environment like YourKit for Java, JProfiler for Java, etc to tools for larger distributed and clustered systems with large number of nodes like CA Wiley Introscope for Java, HP Sitescope and ClearStone for Java.

Q. How would you go about profiling a Java application?
A. The following example uses an hprof tool that comes with Java to determine the cpu times. The profiles are dumped to “java.hprof.txt” file when the program exits or a control character Ctrl-\ or Ctrl-Break (WIN32) depending on platform is pressed. On Solaris OS and Linux a profile is also generated when a QUIT signal is received (kill -QUIT ). The process id can be determined with the “jps” command. The command shown below executes the "ProfilingTest" class with the hprof agent for measuring cpu times.

java -agentlib:hprof=cpu=times test.ProfilingTest 

The profiled results will be dumped to a file named “java.hprof.txt”.

CPU TIME (ms) BEGIN (total = 1953) Thu Sep 15 12:26:50 2011

rank  self    accum      count   trace      method
   1   51.20%  51.20%       1   301025   ProfilingTest.invokeMethod3
   2   31.23%  82.44%       1   301015   ProfilingTest.invokeMethod2
   3   15.21%  97.64%       1   301005   ProfilingTest.invokeMethod1
   4   0.82%   98.46%       1   300396   java.net.URLClassLoader$1.run
   5   0.77%   99.23%       2   300707   java.io.Win32FileSystem.normalize
   6   0.77%   100.00%    268   300309   java.lang.StringBuilder.append
CPU TIME (ms) END
 
PerfAnal is a GUI based analysis tool to analyze the above results.

java -jar PerfAnal.jar  java.hprof.txt


Similar approach can be used for profiling for memory usage.


Learn more -- Java Interview Questions and Answers - performance testing your Java application

Labels:

4 Comments:

Blogger bhanu said...

Excellent..Thank you sir..:)

4:03 AM, February 15, 2013  
Anonymous Vishwas said...

Nice One Sir

9:16 PM, December 06, 2013  
Blogger peerbasha p said...

Good...

10:19 PM, May 22, 2014  
Blogger Arulkumaran Kumaraswamipillai said...

Thanks peerbasha

11:22 PM, May 22, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home