39

I've recently been writing a lot of code in C and am now switching over to Java. I'm currently implementing a large data structure and was wondering whether there were any optimization flags I can turn on when I invoke the Java compiler in order to improve performance like in gcc.

I'm used to:

gcc -O3 -NDEBUG MyProgram.c

is there an analogous command for javac?

I'm using JDK and am running Ubuntu 10.04.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
themaestro
  • 13,750
  • 20
  • 56
  • 75
  • In this case the version of the JDK doesn't matter but in some questions it will. You can use `java -version` to get the exact version. – Peter Lawrey Feb 14 '11 at 22:42

5 Answers5

33

Optimization in Java is mostly done by the JIT compiler at runtime. Hence there is no point trying to instruct it to optimize a certain way at compile time (when it is creating only bytecode anyway). The JIT will almost surely make better decisions on the spot, knowing the exact environment and observing the actual patterns of execution of specific parts of your code.

There are some specific compiler options affecting performance, but these are for tuning the JVM (including the garbage collector) rather than optimizing the code itself.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • Actually, given that we have not only HotSpot and desktop JVM's but also Dalvik (which have dumb JIT), there is much more sense to perform optimizations at compile time (but I'm not saying it should be done by stock javac). – om-nom-nom Apr 06 '13 at 22:43
  • The answer from @locka on Proguard is closer to the use case mentioned in the question. There's a lot the compiler knows and could optimize at compile, but Sun (and now Oracle) decided to leave most of optimizations for JIT, at run time. Proguard seems to fill in the gap in this regard, for a certain extent. For those coming from "close to metal" languages, I guess that Proguard is a better answer to this question. – Richard Gomes Feb 21 '15 at 19:11
20

There's no equivalent to -O3 or any of the -O levels, but there are specific tuning options you have access to via -XX:. There's a full list available here.

This assumes you are using the Oracle-provided JVM, however, and it may differ based on your environment ("using JDK" doesn't really describe which version you are using)

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • 3
    +1 for the link of tuning options – Noremac Feb 22 '13 at 14:53
  • 2
    +1 Thanks for the link. But, unfortunately, those "performance options" (not really "optimization" options) are mostly related to the way GC works or, if not that, related to how memory is allocated/used, which again is related to "garbage" or ways to reduce it. – Richard Gomes Feb 21 '15 at 14:28
16

You can optimize Java byte code by running it through Proguard, a popular Java obfuscator. It will shrink, optimize, and obfuscate code in that order with switches to control pretty much everything. It will remove unused methods and variables, inline code, remove dead branches, merge identical code and perform dozens of other optimizations. The result is a jar file which does the same as your input but takes up less space and runs faster.

It's a must-have for commercial client-side software.

locka
  • 5,809
  • 3
  • 33
  • 38
4

Java code optimisations are applied during runtime as the Hotspot VM sees fit. (As far as I know, this involves keeping track of method invocation counts and compiling frequently called methods into native code and trying several types of optimisations as well.)

The only remotely connected switch you can use with javac is -g:none which tells the compiler to omit debug information: this doesn't really affect the performance of your code but it decreases the size of the .class files.

When you start the VM though, you have a choice between -server and -client options, which will affect the range of optimisations and how early they kick in.

biziclop
  • 48,926
  • 12
  • 77
  • 104
1

The JVM and JIT will do most of optimizations for your code, at runtime, so you will have to bite the bullet and trust the underlying bytecode manipulators (JVM and JIT) to do the optimizations.

darioo
  • 46,442
  • 10
  • 75
  • 103