Reducing static binary size
Andrew Haley
aph@cambridge.redhat.com
Tue Feb 19 09:08:00 GMT 2002
Geert Bevin writes:
> Hello,
>
> I'm evaluating to use gcj for an opensource project instead of plain old
> c. The merits would be that development in java is much cleaner, offers
> faster development and better supports OO. It's however required to be
> able to provide a statically linked binary that doesn't rely on
> anything. This mainly for system rescue purposes. When trying out gcj
> 3.0.3 and compiling HelloWorld.java like this :
>
> gcj -static --main=HelloWorld -o HelloWorld HelloWorld.java
>
> I get an executable of 5752548 bytes, stripping that gets it down to
> 2064192 bytes. Surely this contains a huge lot of dead code for
> something as simple as printing one string to the standard output.
>
> It there a way to reduce the size of the generated binary even further,
> or does someone have any ideas about how to look into that?
Consider Java's support for reflective programming. For example, you
can use Class.forName() to create new instances of a class. Or, at
runtime you can download code from the Internet that may call anything
in the library. The linker can't predict what you are going to do.
So, it might be possible to produce small static binaries but only by
restricting what the program can do at runtime. It might be a nice
idea to create a "tiny Java" subset that doesn't support reflection at
all. The trouble is that libgcj necessarily uses reflection
internally.
I'm sure it's possible to create a version of libgcj that is stripped
down and very much smaller.
Andrew.
More information about the Java
mailing list