Trust memory related issues

This is the code for my program

use actix_web::{App, HttpServer};

async fn main() {

async fn web() {
    HttpServer::new(|| {
            "WEB LISTENING TO  {}:{}",
        // 在这里传入定义的服务
        .bind(("", 18080))

use actix_web::{get, HttpResponse, Responder};
use std::collections::HashMap;
async fn check() -> impl Responder {
    let mut  h: HashMap<i64,i64> = HashMap::with_capacity(100000);
    test(&mut h).await;

    HttpResponse::InternalServerError().body(format!("{:?}", h.to_owned()))

async fn test (h : &mut HashMap<i64,i64>){

// tokio::time::sleep(tokio::time::Duration::from_secs(25)).await;
    for v in 0..1000000{

When the project starts, the memory is very small

The memory just won't fit anymore

What's going on here

How is 21MB an issue? Even a single python process with no program loaded almost takes that much memory. Storing 1000000 pairs of 8bytes + 8bytes takes 15MB already. 15MB + 4MB baseline at startup is 19MB, leaving 2MB unaccounted. Most of this is likely HashMap overhead (HashMap's grow before they are fully filled to reduce hash collisions) As for why this memory is still consumed after dropping the HashMap, memory allocators generally don't immediately release freed memory back to the OS in case the program needs more memory in the near future. Some allocators release memory back to the OS once a certain threshold of unallocated memory is reached, while others do so every so often in a background thread.


Thank you for your answer.I understand what you mean If the memory allocator will not immediately release the operating system in case it is needed in the future, can I understand that if the second or third request is to increase the memory by a small amount on the basis of 21MB, because it is a reused memory? However, the actual situation is not like this. As I have requested multiple times, the memory is increasing

I am requesting serially, not in parallel

Note that if you're looking for help in using rust in your own program, you'll have better luck on

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.