Want to Optimize Your Java Code? Shrink the Bytecode

Wednesday Dec 2nd 2009 by Raghu Donepudi

ProGuard makes the simple yet time-consuming activities of Java code cleanup and optimization relatively trivial—even in large-scale Java development projects.

Software development is much like the game of Whac-a-Mole for programmers. Most of their time is spent addressing requirement changes and their implications. As a result, many lines of code get either commented out or go unused. With deadlines constantly looming, hardly any time is set aside for cleaning up or optimizing the code.

This is where a simple command-line tool called ProGuard comes in. ProGuard is an open-source Java class file shrinker, optimizer, obfuscator, and pre-verifier. Using simple command-line options, you can identify all the dead code and optimize all your code, including third-party JAR files. You can clean up compiled code right after it compiles or clean up the compiled JAR files before deployment. The result is smaller, more compact code archives.

As several commercial Java obfuscators are available, this article focuses on the shrink functionality offered by ProGuard. It shows how to use ProGuard to find all the dead code in your codebase and then shrink the JAR files.

Code Shrinking and Optimization

Code shrinking reduces the size of Java class files when your application does not use all of the bytecode. The shrinking engine analyzes the bytecode to find the code that cannot be reached from a set of given code entry points. The removal of these obsolete code fragments (either entire classes or single methods and fields) is called code shrinking. Shrinking involves other tasks such as making subroutines inline as well. If your application uses well-designed third-party tools, the shrinking engine will remove all other functionalities except the ones used by your application—without compromising any functionality.

However, you need to be careful when shrinking third-party tools as it might violate the support license requirements. ProGuard provides a keep option to prevent the shrinking engine from shrinking the code specified with this option. You can specify class names or method names with this option as well. Make sure to use the keep option for any code that you intend to share with other applications in the future.

The code optimization process identifies and removes calls to methods that are either user written or calls to system methods, if it finds that the return values are not used. ProGuard analyzes the entire hierarchy of each method automatically before removing it. Code optimization is a complex process that requires expert analysis. Use this option with care.

Code Shrinking with ProGuard

When working with various configuration parameters is a hassle, ProGuard provides a simple GUI to add JAR files and generate the output files. You could use the GUI to generate the configuration file from all the selected options.

In the example code for this article, the original size of the input JAR file is 2KB, with one unused class and several lines of dead code intentionally left in the source Java file. After processing through ProGuard, the size of the JAR file decreased to 1KB (a 50% reduction in size).

Figure 1 shows the output of ProGuard displaying snippets of the dead code. In Test.java, ProGuard correctly identified the two variables and the two methods that are part of the unused code.

Figure 1. Snapshot of ProGuard Output Showing Dead Code: ProGuard correctly identified the variables and methods that are part of the unused code.

Figure 2 shows the configuration file used for this example.

Figure 2. Configuration Parameters Used in this Example: The ProGuard shrinking does not impact the JAR files' functionality.

The ProGuard shrinking does not impact the JAR files' functionality. If you are not comfortable with the shrunken JAR files, use ProGuard to identify the dead code and perform a manual code cleanup. Then you can proceed with the regular build process.

ProGuard Annotations

ProGuard provides features to define your own set of annotations and incorporate them into your code. This feature eliminates the need to keep the list of files and methods that you want ProGuard to keep from shrinking. The annotations provided in the ProGuard examples section should suffice for most Java projects though.

Code Cleanup Made Easy

When programmers are under constant deadline pressure, something has to give—especially those things that do not provide immediate value, such as dead code cleanup. Even though programmers have the intention to come back and clean up the code, they seldom do. With ProGuard on hand, that does not have to be the case anymore.

Code Download

  • JavaCodeShrinking.zip
  • For Further Reading

  • "ProGuard Introduction" (from proguard.sourceforge.net)

    About the Author

    Raghu Donepudi is an independent contractor. He works as a technical manager for a U.S. government agency. He holds a master's degree in computer science from Lamar University in Texas. He is a Sun-certified Java developer who has authored a number of innovative software design techniques. Reach him at dsraghu@hotmail.com.
    Mobile Site | Full Site
    Copyright 2017 © QuinStreet Inc. All Rights Reserved