r/programming • • Oct 21 '17

Using Java 9 Modularization to Ship Zero-Dependency Native Apps

https://steveperkins.com/using-java-9-modularization-to-ship-zero-dependency-native-apps#.Wes5IMWbUWE.reddit
310 Upvotes

109 comments sorted by

View all comments

29

u/mcguire Oct 21 '17
cli 21.7 MB

For comparison, the full JRE on this platform is 203 megabytes.

A “Hello World” CLI written in Go compiles to around 2 MB. 

As an aside, "hello world" in C or another true-native language using the system's standard C library is a few KB.

8

u/blobjim Oct 22 '17

Hello world in Java, excluding the VM, is around 400-500 bytes, and it can run on any platform with a JVM. The reason why hello world in native code is so small is because the interpreter is a piece of hardware (which probably has its own set of microcode) and comes with every computer. The JVM is just like a CPU, but is completely implemented in software, so of course it will take more space. If you had an operating system that ran Java code (like JavaOS was, I think?), then you have no JVM size overhead. It's really all about what you offload to where.

32

u/pjmlp Oct 21 '17

Only when dynamically linked.

31

u/TexasCrowbarMassacre Oct 21 '17

A statically linked "hello world" is around 20KB using musl. Using glibc brings it to 700KB.

7

u/[deleted] Oct 21 '17

Have some karma for mentioning musl!

6

u/[deleted] Oct 21 '17

On windows and macOS you cannot link the libc statically. On Linux you can’t do that with the most common libc, glibc. Even still you can easily call the write syscall on Linux without any libc.

4

u/[deleted] Oct 21 '17

I thought so too, but there appears to be the option /ML for the VS compiler that seems to be doing just that. I cannot find anything similar for MinGW though.

2

u/pjmlp Oct 22 '17

Sure you can, better learn to use compiler flags on Windows and alternatives to glibc on Linux.

4

u/killerstorm Oct 21 '17

Not really, you can avoid using libc functions and use OS functions directly.

6

u/vytah Oct 21 '17
section .data
    msg db      "hello, world!"

section .text
    global _start
_start:
    mov     rax, 1
    mov     rdi, 1
    mov     rsi, msg
    mov     rdx, 13
    syscall
    mov    rax, 60
    mov    rdi, 0
    syscall

5

u/Falmarri Oct 22 '17

Now place it inside the ELF header

1

u/Scroph Oct 22 '17

In D, it's roughly 800 KB when compiled with dmd on a 32 bit Linux distro.