3

I recently read on Jon Masamitsu's Weblog that huge methods (8000 bytes of bytecode) are not JIT compiled with HotSpot.

So my question is: how do I find out (as a programmer) how many bytes of bytecode a particular method has?

The JIT compiler of course seems to know. Can I extract this piece of information from the .class file?

jeha
  • 10,562
  • 5
  • 50
  • 69

1 Answers1

7

You can use javap -c mypackage.MyClass to dump the bytecode of your class (and see the size of each method)

Generally speaking, you should know that a method is too large to read and understand before you hit this limit. IMHO its more a problem for generated code. BTW There is a hard limit of 65536 bytes in a method.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Yep, an 8k+ byte method should be several hundred lines of code large which seems exceedingly unlikely in non-generated code. – Voo Jul 05 '12 at 13:19
  • I can decompile it that way, but could you please elaborate how to "see the size of each method"? – jeha Jul 05 '12 at 14:00
  • 1
    When you decompile a method it shows you the offset of each instruction. Look at the offset of the last instruction. – Peter Lawrey Jul 05 '12 at 14:02
  • This means if e.g. "424: areturn" is the last instruction, 424 is the total number of bytecodes? – jeha Jul 05 '12 at 14:08
  • Close. The length of `areturn` is 1, so add one. – Peter Lawrey Jul 05 '12 at 14:09
  • 1
    @Voo It's easy to accidently generate huge methods from small amounts of source code by abusing array initializers. There's one method in the JRE that's over 28kb because of overuse of array initializers. Finally blocks are even bigger potential source of code bloat, but they're rarely used. – Antimony Aug 04 '13 at 16:25
  • If you want to see private methods as well, add ```-private``` – Alex Rothberg Aug 29 '13 at 15:05