Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a Kotlin native executable larger than the equivalent Rust executable?

I created 2 simple "Hello World!" programs, one with Kotlin and one with Rust:

Kotlin:

fun main() {
    println("Hello, world!")
}

Rust:

fn main() {
    println!("Hello, world!");
}

I generated the executable files for both using: kotlinc-native main.kt for Kotlin and cargo build --release for Rust, then checked the binary sizes using ls -S -lh | awk '{print $5, $9}'.

I found that the file generated by Kotlin native is 1.48X the size of the file generated by Rust.

Why does this variance exist?

$ ./program.kexe
Hello, world!
$ ls -S -lh | awk '{print $5, $9}'

835K program.kexe
43B main.kt

$ ./rust
Hello, world!
$ ls -S -lh | awk '{print $5, $9}'

565K rust
128B deps
104B rust.d
64B build
64B examples
64B incremental
64B native

Moreover Rust can be optimized to be smaller, Is there something simliar in Kotlin native?

Initial setup:

$ cargo new hello_world

Build with:

$ cargo build

=> 589,004 bytes

Optimization Step 1:

Build with:

$ cargo build --release

=> 586,028 bytes

Optimization Step 2:

Change contents of  main.rs  to:

 use std::alloc::System;

#[global_allocator]
static A: System = System;

fn main() {
    println!("Hello, world!");
}

=> 335,232 bytes

Optimization Step 3:

Add below to Cargo.toml .

[profile.release]
lto = true

=> 253,752 bytes

Optimization Step 4:

Strip executable via

$ strip target/release/hello_world

=> 177,608 bytes

So, we ended up having the file generated by kotlin native is 4.87X (~ 5X) the file generated by rust

like image 370
Hasan A Yousef Avatar asked Dec 10 '25 17:12

Hasan A Yousef


1 Answers

Rust doesn't have Garbage Collector

like image 153
nkheart Avatar answered Dec 13 '25 17:12

nkheart