I have a simple TCP echo server using standard library:
use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind("localhost:4321").unwrap();
loop {
let (conn, _addr) = listener.accept().unwrap();
std::io::copy(&mut &conn, &mut &conn).unwrap();
}
}
It uses about 11 MB of memory:
Tokio
If I convert it to use tokio:
tokio = { version = "0.2.22", features = ["full"] }
use tokio::net::TcpListener;
#[tokio::main]
async fn main() {
let mut listener = TcpListener::bind("localhost:4321").await.unwrap();
loop {
let (mut conn, _addr) = listener.accept().await.unwrap();
let (read, write) = &mut conn.split();
tokio::io::copy(read, write).await.unwrap();
}
}
It uses 607 MB of memory:
async_std
Similarly, with async_std:
async-std = "1.6.2"
use async_std::net::TcpListener;
fn main() {
async_std::task::block_on(async {
let listener = TcpListener::bind("localhost:4321").await.unwrap();
loop {
let (conn, _addr) = listener.accept().await.unwrap();
async_std::io::copy(&mut &conn, &mut &conn).await.unwrap();
}
});
}
It also uses 607 MB of memory:
Why do the asynchronous versions of the program use 55x more memory than the synchronous one?
valgrind --tool=massif
is able to give you heap snapshots with the total allocated per allocation site. – Matthieu M. Aug 4 '20 at 9:16VIRT
column shows the amount of address space assigned to the process. TheRES
column shows the amount of address space mapped to physical memory. Saying the process "uses" 607 MB of memory just because it gets that much address space seems a bit off. – Sven Marnach Aug 4 '20 at 9:25pmap
shows several ~64 MB blocks00007f617c021000 65404K ----- [ anon ]
. – Gurwinder Singh Aug 4 '20 at 10:11