(Note: this a a copy of my StackOverflow question)
I looked at how much RAM was used by Rust programs (with the top
and massif
commands) and I wonder why they use so much memory.
Here is an example:
use std::io;
fn main() {
println!("What's your name?");
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
println!("Hello {}!", input);
}
I saw that 6 MB of memory was used before I input something.
Here is how I compiled and executed the program:
cargo build --release
./target/release/main
The equivalent C program:
#include <stdio.h>
int main(void) {
printf("What's your name?\n");
char input[100] = {0};
scanf("%s", input);
printf("Hello %s!\n", input);
return 0;
}
only uses 0.6 MB. In this case, the Rust program uses 10 times more memory. In other cases, I saw that the Rust program uses 5 times more memory.
I also tested with other languages to compare.
The OCaml version:
let () =
print_endline "What's your name?";
let line = read_line () in
print_string "Hello ";
print_endline line
uses 1 MB.
The Haskell version:
main = do
putStrLn "What's your name?"
name <- getLine
putStrLn ("Hello " ++ name ++ "!")
uses 3 MB.
The Python version:
print("What's your name?")
name = input()
print("Hello", name, "!")
uses 7 MB, almost the same as the Rust version!
I'm running Linux (ArchLinux) with Rust 1.3 (I also tried the nightly with similar results).
Here is more data from the htop
command:
VIRT RES SHR MEM% Command
15572 2936 804 0.1 ocaml
21728 2732 2528 0.1 haskell
22540 7480 4308 0.2 python
4056 668 600 0.0 c
24180 6164 1928 0.2 rust
13060 1524 1387 0.0 cpp
Here are massif
results.
For every program, I ran massif
twice, as following:
valgrind --tool=massif --time-unit=B ./program
valgrind --tool=massif --pages-as-heap=yes --time-unit=B ./program
Here are the results with all the programs (as shown by ms_print
):
(links on the StackOverflow question - sorry, I cannot post more than 2 links since I am a new user)
Summary (ram usage):
|------------|----------|----------|----------|----------|----------|----------|
| | C | Haskell | OCaml | Rust | Python | C++ |
|------------|----------|----------|----------|----------|----------|----------|
| First run | 1 B | 63.12 KB | 5.993 MB | 816 B | 1.321 MB | 71.10 KB |
|------------|----------|----------|----------|----------|----------|----------|
| Second run | 6.031 MB | 24.20 MB | 17.14 MB | 25.60 MB | 27.43 MB | 14.74 MB |
|------------|----------|----------|----------|----------|----------|----------|
The first run is without the --pages-as-heap=yes
parameter.
I also ran massif
with the --stacks=yes
option for C, C++ and Rust:
C version:
(link on the StackOverflow question)
(3.141 KB)
C++ version:
(72.63 KB)
Rust version:
(link on the StackOverflow question)
(8.602 KB)
What does explain such a huge difference between heap block allocation and page allocation in Rust?