How can I trace a Java program performance? Example, how long each method takes? How many resources were used and so on? I need some info for me to work on optimizing my Java program.
As others previously mentioned, profilers are the go. A long time ago, I'd used http://www.yourkit.com/, and found it quite easy to use and informative.
If you are keen, you could investigate using AOP for method timing etc. Just search Google for AOP method timing for some ideas.
You could try opening your project in Netbeans, from there you can use the Profiler tool and get performance data for methods, load times, etc. It's really easy to use and the data is very complete.
Netbeans Profiler
You can try a JVM profiling tool such as JProfiler.
Making a good and robust benchmark in java is hard. Take a look at the following articles:
http://www.ibm.com/developerworks/java/library/j-benchmark1/index.html
http://www.ibm.com/developerworks/java/library/j-benchmark2/index.html
In optimizing, it's hard to tell what causes the slow performance. Usually a bad algorithm (complexity) causes it. Maybe you can start from the algorithm first before go into further detail tweak
Related
What is the easiest way to track down (i.e., find the cause of) a 'GC overhead limit exceeded' error?
What I do not consider good options:
Adding the -XX:-UseGCOverheadLimit parameter to the JVM call. That Java exception is telling me there is something incredibly inefficient in my implementation, and I want to fix that.
"Go and look carefully at your code". The project is very large, so I need some clues regarding where to look for inefficiencies.
Shoud I use a profiler? If yes, which one would you suggest?
Should I look into the GC log? I have tried doing that, but I have a little understanding of it, and it seems there are no clear pointers to the code (saying which objects are being GC'ed).
Many questions have been asked on SO about this error, but no one seems to answer this specific question.
Simplest tools to start profiling your app
Netbeans comes with a built in profiler.
Jconsole can also help a bit
VisualVm can also aid a bit.
Commercial Tools that is really awesome is DyanTrace
Now for the approach to fix your problem:
Although there can be other ways you can tackle it. but following things may help.
1) The symptoms that you are seeing are probably result of creation of too many short lived objects in your code. Now this is not a memory leak situation but too much garbage to clean for the JVM. And Jvm is failing to keep up with that. You need to check your code for where are these objects getting created.
2) Second thing you can do is take several heap dumps at regular intervals between two GC runs and compare these heapdumps in netbeans or some other tool of your choice. you need to do this before your app goes into this bad state.This comparison will tell you what has grown in heap and may be will give you a pointer where to look into your code.
I hope this helps in solving your issue. :)
I want to optimize my Java source code, but I am not sure what parts of my code may be optimized. Is there any tool or any technique through which I can optimize my Java source code?
I know the basic fundamentals of code optimization, but I have a large number of lines and very complex source code (Java classes).
Can I optimize my code with the help of a Java profiler?
Thanks in Advance!
What do you mean by optimising your code? DO you want to refactor your code to simplify it. In that case I suggest you use your IDE to help refactor the code.
If you want to minimise its memory / CPU consumption, I would suggest you use a profiler like VisualVM or YourKit to idenify where resources are being consumed.
A code analysis tool can also help pick up the obvious performance issues. I have code analysis on as I type in my IDE which helps me pick up those issues as I write it.
Performance optimization - yes, a profiler may help. At least it can show you those areas in your application that take an unexpected amount of CPU time.
But before starting to apply changes - take care not to do some microoptimization. Look at improving algorithms first. Sometimes we use nested for loops while a task can be done with a single one (linear time). Double check if you use the correct collection types. Then have a look if you accidentally create more objetcs than needed (object creation in loops is a typical reason for performance problems).
Their are several tools for static code analyzes (to do code style / code convention / best practises / bug "optimization" of your code).
Checkstyle
PMD
Findbugs
I would recommend using Sonar. It covers them all and is easy to setup - even on a local machine (unzip and start). It is best used with maven projects but also possible for NON maven projects.
I'm kinda confused as to where to start looking to optimize my java-based application. Can someone give me some guidance as to what to look for?
Add note:
The java application I'm running is open source but I don't want to tweak it myself as I'm not capable of doing it. So what I'm looking at is on how to optimize the execution environment so as to fit the current behavior of the app. By the way, the application is hudson, a java-based continuous integration system.
Thanks in advance :)
Before start optimizing try to find out where you do you have a problem. Is your application CPU bound, memory-bound, or I/O bound? When you know this, try to find the biggest performance impact first and try to optimize it. Use Java profilers to find the problems in your application or configuration.
A good starting point for the whole process could be the Java Performance Tuning site.
Official Java Performance Documentation and Java SE Performance at a Glance
Read Java HotSpot VM options performance tuning.
Try unlock experimental VM options(-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC
-XX:+UseFastAccessorMethods).
Optimizes the code.
Before you attempt to optimise the JVM, I suggest you optimise your code. You can use a profiler like VisualVM (free) or YourKit (commercial) to look at CPU and memory performance bottleneck. Often simple changes can make a big difference.
Conversely, you can change lots of JVM options and not be sure its really helped.
If we have 300 classes in an application, is it possible to monitor how many instances of each class we have at a given time? Is it possible to know how much memory each instance is consuming?
Thanks
JDK 1.6 includes a tool called jvisualvm, which allows you to view lots of information about your running Java program, including memory usage, threads, etc. You could also use a profiler to see this kind of information. The profiler in NetBeans looks a lot like JVisualVM.
I personally like Yourkit. It has a very good UI and comes with a 30 day trial. The details are also pretty extensive.
The online help document in that site should help you on how to set things up for running it.
use profiler4j or pmd
personally i like profiler4J for its ease of use and simple graphics :)
use jvisualvm.exe it is part of the JDK6
Most profilers will give you this information. I'm personally familiar with JProfiler, but I expect any worthwhile profiler would let you do this.
For a more low-tech solution, you could even trigger a heap dump from your application and then look through it with an application like jhat. The interface leaves a lot to be desired, though, and profilers would be much more comfortable to use in any non-trivial case.
Edit: here is an example of the memory screen for JProfiler, and you can also investigate the reference chain.
You could use a Java profiler, depending on which web container (if it's a web-app) you're deploying to you can try alot of different profilers: http://java-source.net/open-source/profilers
I am reviewing a big java application to see if there are any performance bottlenecks. The real problem is that I cannot pinpoint the performance issues to any single module. The whole application is slow as such.
Is there some tool/technique I can use to help me out in this?
Try using a profiler on your running code. It should help you identify the bottlenecks. Try jprofiler or Netbeans profiler
I'm often happy enough using Java -Xprof. This gives you a sorted list of the functions your code spends most of its time in.
If you are running on Java 6 you can use the supplied monitoring tools
For testing/development purposes, you can download Oracle JRockit Mission Control for free from this site. (Requires Login, but accounts can be set up with any email adress)
Docs Here. It will allow you to find hotspots, memory leaks and much more.
As we see from How can I profile C++ code running in Linux?, the most statistically significant approach is to use a stack profiler.
Well, Java runs in the JVM, so getting a stack useful for C code won't be useful for us (it'll get the JVM stuff, not your code). Fortunately, Java has jstack! http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jstack.html
It'll give you a bunch of threads, like the GarbageCollector. Don't worry about those, just look at where your threads are.
YourKit is a excelent java profiler (not free).