title: Why is a Kotlin native executable larger than the equivalent Rust executable?
body: <p>I created 2 simple "Hello World!" programs, one with Kotlin and one with Rust:</p>
  
  <p>Kotlin:</p>
  
  <pre><code>fun main() {
      println("Hello, world!")
  }
  </code></pre>
  
  <p>Rust:</p>
  
  <pre class="lang-rust prettyprint-override"><code>fn main() {
      println!("Hello, world!");
  }
  </code></pre>
  
  <p>I generated the executable files for both using:
  <code>kotlinc-native main.kt</code> for Kotlin and <code>cargo build --release</code> for Rust, then checked the binary sizes using <code>ls -S -lh | awk '{print $5, $9}'</code>.</p>
  
  <p>I found that the file generated by Kotlin native is 1.48X the size of the file generated by Rust.</p>
  
  <p>Why does this variance exist?</p>
  
  <pre class="lang-none prettyprint-override"><code>$ ./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
  </code></pre>
  
  <p>Moreover Rust can be optimized to be smaller, Is there something simliar in Kotlin native?</p>
  
  <p><strong>Initial setup:</strong></p>
  
  <p><code>$ cargo new hello_world</code></p>
  
  <blockquote>
    <p>Build with:</p>
  </blockquote>
  
  <p><code>$ cargo build</code></p>
  
  <p>=> <code>589,004 bytes</code></p>
  
  <p><strong>Optimization Step 1:</strong></p>
  
  <blockquote>
    <p>Build with:</p>
  </blockquote>
  
  <p><code>$ cargo build --release</code></p>
  
  <p>=> <code>586,028 bytes</code></p>
  
  <p><strong>Optimization Step 2:</strong></p>
  
  <blockquote>
    <p>Change contents of  <code>main.rs</code>  to:</p>
  </blockquote>
  
  <pre><code> use std::alloc::System;
  
  #[global_allocator]
  static A: System = System;
  
  fn main() {
      println!("Hello, world!");
  }
  </code></pre>
  
  <p>=> <code>335,232 bytes</code></p>
  
  <p><strong>Optimization Step 3:</strong></p>
  
  <blockquote>
    <p>Add below to <code>Cargo.toml</code> .</p>
  </blockquote>
  
  <pre><code>[profile.release]
  lto = true
  </code></pre>
  
  <p>=> <code>253,752 bytes</code></p>
  
  <p><strong>Optimization Step 4:</strong></p>
  
  <blockquote>
    <p>Strip executable via</p>
  </blockquote>
  
  <p><code>$ strip target/release/hello_world</code></p>
  
  <p>=> <code>177,608 bytes</code></p>
  
  <p>So, we ended up having the file generated by kotlin native is 4.87X (~ 5X) the file generated by rust </p>