Finding the Bytecode Size of a Method

旧巷老猫 提交于 2019-12-01 04:10:15

How can I tell the bytecode size of that method?

One way is to just add them up :-)

Each bytecode instruction consists of 1 byte for the primary instruction plus a fixed number of operand bytes.


A more practical way is to dump the classfile containing the bytecodes using javap -c. The output includes byte offsets for each instruction.

Reference: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html


1) I can add ALOAD 0 ASTORE 4 as 4 bytes, but what do I do with with ARRAYLENGTH or INVOKESTATIC method-name?

The instructions are listed in Section 6.5 of the JVM spec - http://docs.oracle.com/javase/specs/jvms/se7/html/index.html

  1. Scroll down to the relevant part of the index.
  2. Click on the link for the instruction.
  3. Read the "format" and "description" to figure out how many bytes are used.

Following this procedure, I deduced that ARRAYLENGTH is 1 byte, and INVOKESTATIC is 3 bytes.

2) I tried to use javap but for some reason I get class not found (it's inside a jar and I passed -classpath filename.jar to javap but it didn't work).

Read the javap manual entry again. It does work if you use it correctly. (Perhaps you didn't supply the fully-qualified classname in the correct format.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!