Mihai's page

Is Rust Eating Too Much Memory?

A few months ago, on one of the Discord servers I am in, someone mentioned that Rust might be asking for too much memory at runtime, compared to the equivalent C program. I wanted to test that and ran a simple experiment. Here, I just report that experiment from September, as I was keeping this as a draft, planning to expand the analysis to larger programs and looking at details. Instead, that will happen at a later time.

I took the simplest possible program: just print a hello world message. In C:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    puts("Hello world!");
    return 0;
}

And in Rust:

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

Then, I compiled these in the same Nix shell:

[nix-shell:/tmp/rust-c]$ time gcc -Wall -Wextra hello.c -o hello-gcc

real    0m0.242s
user    0m0.174s
sys    0m0.066s

[nix-shell:/tmp/rust-c]$ time clang -Wall -Wextra hello.c -o hello-clang

real    0m2.227s
user    0m0.269s
sys    0m0.239s
[nix-shell:/tmp/rust-c]$ time rustc hello.rs -o hello-rust

real    0m0.487s
user    0m0.292s
sys    0m0.205s

Clang seems to be taking quite a significant amount of time here, compare to the other 2 (GCC and Rust).

This resulted in 3 new binaries on disk:

[nix-shell:/tmp/rust-c]$ ls -l hello-*
-rwxr-xr-x 1 mihai users  15808 Sep 22 20:25 hello-clang
-rwxr-xr-x 1 mihai users  15680 Sep 22 20:25 hello-gcc
-rwxr-xr-x 1 mihai users 505008 Sep 22 20:26 hello-rust

The Rust binary is much much larger than the C ones, potentially due to static linking.

Next, running these binaries using the memusage script, we have:

[nix-shell:/tmp/rust-c]$ memusage ./hello-gcc 
Hello world!

Memory usage summary: heap total: 1024, heap peak: 1024, stack peak: 608
         total calls   total memory   failed calls
 malloc|          1           1024              0
realloc|          0              0              0  (nomove:0, dec:0, free:0)
 calloc|          0              0              0
   free|          0              0
Histogram for block sizes:
 1024-1039            1 100% ==================================================

[nix-shell:/tmp/rust-c]$ memusage ./hello-clang 
Hello world!

Memory usage summary: heap total: 1024, heap peak: 1024, stack peak: 608
         total calls   total memory   failed calls
 malloc|          1           1024              0
realloc|          0              0              0  (nomove:0, dec:0, free:0)
 calloc|          0              0              0
   free|          0              0
Histogram for block sizes:
 1024-1039            1 100% ==================================================

[nix-shell:/tmp/rust-c]$ memusage ./hello-rust 
Hello world!

Memory usage summary: heap total: 2976, heap peak: 1736, stack peak: 1264
         total calls   total memory   failed calls
 malloc|          4           2640              0
realloc|          3            184              0  (nomove:0, dec:0, free:0)
 calloc|          1            152              0
   free|          7           2976
Histogram for block sizes:
   32-47              2  25% ==================================================
  112-127             1  12% =========================
  144-159             1  12% =========================
  240-255             1  12% =========================
  464-479             1  12% =========================
 1024-1039            2  25% ==================================================

Rust also requires much more RAM to run this simple program, performing more allocations, for multiple block sizes (and leaking some memory?).

Now, all this is just a trivial experiment, done just to whet the appetite. A much larger experiment, with a program that does something useful, and analyzing all the differences, is warranted. But that will be in a separate article, when I have some time to create and run these long experiments.

For now, I will leave you with this repo which performs similar comparisons across multiple languages.


Comments:

There are 0 comments (add more):