bastion-rs / bastion

Highly-available Distributed Fault-tolerant Runtime
https://www.bastion-rs.com
Apache License 2.0
2.79k stars 103 forks source link

Memory does not release #344

Open zrus opened 1 year ago

zrus commented 1 year ago

Struggling with Bastion does not release memory after handling messages

Hi,

Now I am working with a project that uses Bastion as a core of actor model architect. I have used it once before but with this project I am running into a problem as I describe. Here is my little demonstration about it:

use std::time::Instant;

use bastion::prelude::*;

#[tokio::main(flavor = "multi_thread", worker_threads = 16)]
async fn main() {
  let start = Instant::now();

  Bastion::children(|children| {
    children
      .with_distributor(Distributor::named("test_actor"))
      .with_redundancy(15)
      .with_exec(|ctx| async move {
        loop {
          MessageHandler::new(ctx.recv().await?).on_tell(|msg: Vec<u8>, _| {
            drop(msg);
          });
        }
      })
  })
  .unwrap();

  let sender = Bastion::children(|children| {
    children.with_exec(|_| async {
      let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(10));
      loop {
        interval.tick().await;
        for _ in 0..3_000 {
          Distributor::named("test_actor")
            .tell_one(vec![0u8; 300])
            .unwrap();
        }
      }
    })
  })
  .unwrap();

  Bastion::init();
  Bastion::start();

  let mut buf = String::new();
  std::io::stdin().read_line(&mut buf).unwrap();
  sender.kill().unwrap();

  println!("Running time: {:?}", start.elapsed());

  let waiting = Instant::now();
  std::io::stdin().read_line(&mut buf).unwrap();

  println!("Waiting time: {:?}", waiting.elapsed());

  Bastion::block_until_stopped();
}

Result After start: memory is about 0.2% After first std::io::stdin().read_line(&mut buf).unwrap();: Running time: 2488.367728948s - memory is about 0.5% After second std::io::stdin().read_line(&mut buf).unwrap();: Waiting time: 792.050614104s - memory is being kept at 0.5%

Sorry for my information that could not be easy to analyze (I am still new to Rust and have not found a tool to measure memory yet 😓).

As you can see if I send 10_000 of messages at a time (each message is about 300 bytes (in practice is more than that)) the issue comes.

Hope someone could help me out of this soon. Did I do something wrong? Thanks in advance.