Class optimizer

I know there’s many small programs on the net for optimizing java class files, like this one :

http://www.uni-vologda.ac.ru/~c3c/jco/

So I would like to have some comments about these softwares and know which software is the best for reducing class size.

Anyone ?

++
Chman

Here http://www.geocities.com/marcoschmidt.geo/java-class-file-optimizers.html is an overview about class optimzing / obfuscating tools. Personally I tried jopt. I could not really determine any performance improvements. The classes were crunched a bit, in average I would say about 20%.

The ofuscating effect was fine so far to make class file debugging harder but by far not impossible.

i’ve used jax6.0 & jax7.3 in the past (for J2ME game development, and a 4k game contest), class size depends very much on the code, but you should expect around a 20-40% reduction in class size.

As 2 speed increase, i’d say its pretty negligable. (except in very extreme circumstances)

p.s. jax is abit out of date now (IBM hasn’t updated it in quite a while) it only supports java1.3 and below.

thanks fos answers…
so class optimizer are only useful for reducing class size…

++
Chman

[quote]so class optimizer are only useful for reducing class size…
[/quote]
Pretty much, an “optimizer” can’t do much useful performance wise. Most real optimization is done by the JIT at run time. You’ll get the most optimization from improving your algorithms.

As for class size an optimizer can strip the debugging info out if you forgot to. Also it can rename private methods and fields, the method name takes up bytecode space, so limiting visibility can help.

Also, when jar’ing the files make sure compression is on, usually is, and you may be able to save a few bytes by including your own manifest instead of the default. Also, recompressing with a zip tool like WinZip with maximal compression enabled may help. I haven’t tried the previous ideas so no guarentees.

Yup, jar.exe that comes with the jdk gives pretty abysmal compression. For best results I would recommend using 7-zip rather than Winzip.

When you use winzip or 7-zip to make jar files,do you just make a normal zip then rename it to .jar?

[quote]When you use winzip or 7-zip to make jar files,do you just make a normal zip then rename it to .jar?
[/quote]
yes. zip and jar is the same (just a different file extension)

btw i use jarg… it’s compression seems to be a bit better than 7-zip’s or winzip’s (in most cases). btw jarg is an obfuscator wich “eats” jar files directly (as u might have guessed - the output is a jar file too).

jarg can be found at sourceforge btw :wink:

jax and jarg seems to be a good combo :slight_smile:

I second that - jarg manages to do even more optimisations than jax :o
(though I find 7zip creates smaller zips than jarg ::))

my compilation process is.

javac->jax->jarg->7zip

btw,
does any1 know if 7zip can be setup to execute from the cmdline? (with 98 not 2k :()

depends :stuck_out_tongue:

[quote]does any1 know if 7zip can be setup to execute from the cmdline? (with 98 not 2k :()
[/quote]
yea 7zip can be used from the cmdline… well the syntax is a pain in the arse (check “7-zip.chm” for details).

add it’s directory to your path variable (autoexec.bat)


//before
SET PATH=C:\j2sdk1~1.1_0\BIN;C:\BORLAND\BCC55\BIN;%PATH%
//after
SET PATH=C:\j2sdk1~1.1_0\BIN;C:\BORLAND\BCC55\BIN;C:\PROGS\7ZIP;%PATH%

after reboot or starting the bat manually u can run the 7z.exe (cmd line version) from every directory.

well… u don’t have to add it to your path variable if u just want to call it from bat files.

"C:\Progs\7zip\7z.exe" a -tzip test.zip -mx a.class readme.txt

a=add
-tzip=use zip compression
test.zip=put it into “test.zip”
-mx=max compression
a.class=a file i would like to add
readme.txt=another file i would like to add

in theory u can even compress a bit better than the default “max compression” default does. if u set fastbits to 255 (fb=255) and passes to 4 (pass=4).

[the default “max” uses fb=64 and pass=3]

well… in reality it seems to make no difference. i tried it with a lot of different files and it was always as big/small as the default max compression preset. but i think there are some files where it would make a difference :slight_smile:

cool :smiley:

if it saves me ‘135 bytes’, consider the favour returned ;D

Functionally zip and jar are the same but there are some minor differences to note.

  1. Both use zlib and make more or less the same kind of files. However if you want to retain cross platform portability you need to take care in making permutations to the compression. Not all platforms come with the same ability to decompress more dense compression formats. Jar uses fairly conservative compression to maintain cross platform portability.

  2. Minor point but jar by default adds a /META-INF and manifest file.