Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding Up Java

This is really two questions, but they are so similar, and to keep it simple, I figured I'd just roll them together:

  • Firstly: Given an established Java project, what are some decent ways to speed it up beyond just plain in-code optimization?

  • Secondly: When writing a program from scratch in Java, what are some good ways to greatly improve performance?

Please stay away from general optimization techniques unless they are Java specific.

I asked this about Python and Perl earlier. For Java I'm wondering what good tips/tricks are out there to improve performance and if there are any particularly good Java profilers.

like image 318
akdom Avatar asked Oct 07 '08 18:10

akdom


1 Answers

Firstly : by code optimization, I would assume that you've done the right algorithms and right implementation of algorithms. In which case, you would use the profiler and look at how often your garbage collector(GC) is collecting garbage and how much time it is using for doing that. Then you start working on the GC options -- but beware you can get into trouble if you don't know what you're doing.
I assume that you're using java 5/6. In which case, I'd go through the java 5 tuning guide at http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html. There is also a very very good newletter on java performance called http://www.javaperformancetuning.com/ to which you can subscribe.

Other than that, look at how many try/catch blocks you can eliminate. See if you can eliminate unnecessary throwing of Exceptions.

Use Caches where you can BUT don't over do it.

Read Effective Java, 1st and/or 2nd Edition

Profilers : I use yourkit. It's pretty good for java 1.5 and above. You can get a personal license. Other profilers are also good as well.

Just like you have unit and integration tests, it does NOt hurt to have some performance tests that you run as part of your CONTINUOUS INTEGRATION(CI) builds. This way you know when you regressed, especially if you are using a good CI build server.

like image 174
anjanb Avatar answered Sep 17 '22 15:09

anjanb