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?