Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I do to make jar / classes smaller?

I'm developing a Java Applet, and reducing the size of the binary code will make the applet open faster and will improve the user experience.

Is there anything I can do to reduce the size of classes and / or jar files? I want to be sure I'm not missing obvious tricks.

I know that in the C++ world compiler options for e.g. stripping debug symbols can make a huge difference, but I've never seen something like that for Java.

like image 220
amarillion Avatar asked Nov 24 '09 13:11

amarillion


2 Answers

check out this page for some tips on how you could make your jar files smaller - http://wiki.java.net/bin/view/Games/4KGamesDesign . Even though some may not apply since you are not trying for absolute minimalization, there are some general tips that you can apply without compromising code quality.

but a summary here:

Keep your code down to one class. Each class adds the overhead of an entry in the JAR file, as well as a brand new constant pool and class list.

Keep your methods to a minimum. Each method adds overhead in the class file. All you should need is a main() method, and methods to implement the keyboard and/or mouse routines.

Don't use global variables. Global variables require special meta-data in the class to identify. Method-local variables, however, are only stack entries and cost nothing extra to use.

Use a good compressor like 7Zip or KZip to create your JAR files. The JAR utility is mostly designed for correctness, not compression ratios.

Use an obfuscator like ProGuard, JoGa, or JShrink to optimize the size of your class.

Use a single character for the class file name. This reduces its size internally, reduces the amount of info the Zip program stores, and reduces the size of the manifest.

Reference as few classes as possible. Each class you reference adds the full package and class name, plus the method signature you're calling.

Redundancy (such as using the same name for all your methods and classes and fields) improves compression ratios.

Methods made private and final can be inlined by a class optimizer.

Use the String.valueOf() method to convert primitives to strings. For example, ""+number expands to: new StringBuffer?().append("").append(number).toString() wasting a great deal of space in new class and method references.

Static strings, floats, and integers used in the source code get stored in the constant pool. As a result, the more you can reuse a static value, the smaller your class will be.

You can make liberal use of static final variables for constants. This will make your code more readable and ProGuard will optimize this away so there is no extra overhead.

like image 178
Chii Avatar answered Oct 18 '22 12:10

Chii


You can use

javac -g:none

to remove debugging information - I don't know how much difference it's likely to make though.

How convinced are you that the download time is the bottleneck? How big is the applet? Unless it's enormous, I doubt that size will make much difference after it's downloaded.

like image 27
Jon Skeet Avatar answered Oct 18 '22 12:10

Jon Skeet