Main page > Java

Java class file optimization and compression tools

Java compilers typically create class files from Java source code. These files contain bytecode (instructions that are interpreted by a virtual machine), constants, debug information and more. The class file format is documented in chapter 4 of the book The Java Virtual Machine Specification by Tim Lindholm and Frank Yellin. While class files are relatively compact, they can be further reduced in size. This is especially interesting when using Java on embedded systems, where space is limited.

Note that optimization on this page generally refers to optimization regarding file size. It is unknown to me whether these size reductions have any negative effect on execution speed. Bytecode optimized for size could make it harder for just-in-time compilers—as used in modern Java runtime environments like the ones from Sun or IBM—to generate fast native code, but this is just a theory. JavaGO (download ZIP) optimizes class files for speed and claims to cooperate well with Sun's Hotspot JIT compiler.

Reducing the size of class files will save

If you want to read or write class files to analyze or manipulate them from within your Java application, try one of the class file libraries listed at ODP.

Class file optimization tools

For class files to become smaller, optimization tools usually rename fields and methods, remove unused attributes and debug information, compress the constant pool, etc.

In addition, Java obfuscators may also support file size reduction functionality. An obfuscator's primary goal is to make reverse-engineering of an application harder. Some of the techniques used to accomplish this task may also have the benefit of smaller files. The Open Directory has a list of Java obfuscator applications.

Data compression

When using JAR archives for application or applet deployment, a size reduction of the JAR file can be achieved by using the maximum compression switch of the archiver application (JAR is the same file format as ZIP, so you can use all applications that support that better known format). 7-Zip claims to create ZIP files that are 2 to 10 percent smaller than the ones created by PKZip and Winzip. Once the class files have been extracted from the JAR archive and loaded into memory, that compression is no longer helpful.

William Pugh has created a dedicated compression tool for sets of class files: pack. He describes it in his paper Compressing Java Class Files (1999, ResearchIndex entry).

Jazz, a similar tool with compression inferior to pack, is described in JAZZ: An Efficient Compressed Format for Java Archive Files (1998).

Acceleration of application and applet startup times

When loading an application or applet via the Internet or an intranet, the time until "something happens on the screen" can be significant. Apart from file size reduction as described above, rearranging class files in a JAR file can speed up that process. Putting classes that are typically required first at the beginning of a JAR file will improve startup time.

David Hovemeyer and William Pugh describe that technique in the paper More Efficient Network Class Loading through Bundling.

Syntax-oriented coding (SoC) is a compression technique for sources that follow grammatical rules. It is implemented for Java class files in jaccelerate, a product by Syntion - they also provide jampagne, which also seems to use some sort of bundling to improve startup time. The two products can be combined.

JSR 200 - Network Transfer Format for Java Archives

This is a Java Specification Request (JSR) dealing with a new network transfer format for bytecode. That new format is supposed to save space when compared to a JAR archive of class files. An implementation of that format will be added to Java 1.5 as a new package javax.pack.