3

Possible Duplicate:
Why are compiled Java class files smaller than C compiled files?

Just out of curiosity I just compiled "Hello Worlds" in C, C++ and Java.

The Java class file comes out very lean at just 423B which I understand since the runtime isn't included in the binary.

The C and C++ ones are 8.5K and 9.2K however.

Why are they so relatively big? I always assumed stdio or iostream are linked at dynamically and don't add to the size of the executable.

So where do all the kilobytes come from? By looking at the hexdump I see that there's a lot of padding, I guess for performance reasons. Why exactly is a binary format organized like that?


pmg's link is very helpful!

Regarding the padding, I found it's the aligning of the program's segments to virtual memory page boundaries (4096 bytes) that causes it to be at least 8192 bytes.

Regarding the mach-o binary format (for OS X and iOS)

For best performance, segments should be aligned on virtual memory page boundaries—4096 bytes for PowerPC and x86 processors. To calculate the size of a segment, add up the size of each section, then round up the sum to the next virtual memory page boundary (4096 bytes, or 4 kilobytes). Using this algorithm, the minimum size of a segment is 4 kilobytes, and thereafter it is sized at 4 kilobyte increments.

quoting http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html

I'll do the research before asking next time ;)

Community
  • 1
  • 1
Phantrast
  • 639
  • 1
  • 9
  • 18
  • 13
    You might like to read [A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux](http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html). – pmg Aug 05 '12 at 08:35
  • @pmg - Thanks for that link. It's an amazing piece of work, very well written. – Ted Hopp Aug 05 '12 at 08:57
  • 1
    This reminds me of the demoscene, where a 64 kB program can open a window with buttons and checkboxes for settings, then play an amazing 3d video, complete with sound, more than 5 minutes long, where even one frame from of that video would be over 64 kB even with reasonable compression if stored as an image file. – vsz Aug 05 '12 at 09:01

4 Answers4

5

This is a question of what you're measuring. If it's the raw executable size, this contains a great deal besides the code for main().

As we're using shared dynamic libraries here, a great deal of the size will be accounted for by house-keeping data such as symbol tables, the global offset table and a description of the shared libraries to be linked against - the code of the shared libraries themselves is not in the binary.

The iostream library is a fairly large, and also has static initialisers - for instance to initialise cout, cerr, and cin objects. This is another thing that object file must contain.

In reality, most of the extra size is not memory resident when the application runs.

marko
  • 9,029
  • 4
  • 30
  • 46
2

The C & C++ is a complete stand alone program. The Java is just the core code and requires another program to run it.

A smaller hello world is to use a bash script (something which also needs another program to run)

echo Hello World

17 bytes in total with a new line.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Yes and no. C and C++ contain executable code, but it still depends on a lot of plumbing: the OS has to parse the ELF file, and separate libraries (the stdlib) still have to be present on the system). But you're right that it doesn't depend on a separate interpreter/compiler to turn the file into executable code – jalf Aug 05 '12 at 09:03
1

because stdlib is included. try using -nostdlib

John
  • 11
  • 1
  • Without the C standard library, the hello world program will either not link or will need to do some IO with raw system calls. A very small binary would result! – marko Aug 05 '12 at 09:09
1

One factor is

#include <iostream>

which causes a lot of the standard library to be linked with your program. However it's no need to worry. It's just an initial overhead, it doesn't increase with the complexity of the program or length of code. In any case, use try using UPX.

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153