Skip to content

In linux(debian 11.2, Alma 8.5) TcpStream after write_all cache to much memory #105293

Open
@tickbh

Description

@tickbh

In Rust 1.65.0 and x86_64-unknown-linux-gnu build, debian 11.2

use std::{time::Duration, io::Write};
use std::net::TcpStream;

fn main() {
    // now memory is 5m
    std::thread::sleep(Duration::new(5, 0));
    {
        for i in 1..10 {
            // any redis clint
            let mut tcp = TcpStream::connect("127.0.0.1:6379").unwrap();
            let data: Vec<u8> = vec![49; 26748528];
            let _ = tcp.write_all(&data);
            std::thread::sleep(Duration::new(5, 0));
        }
    }
    // now memory is 27m
    std::thread::sleep(Duration::new(500, 0));
}

I expected to see this happen: every once run
When I run multi thread, It will increase too much memory. just like

use std::thread;
use std::{time::Duration, io::Write};
use std::net::TcpStream;

fn main() {
    // now memory is 5m
    std::thread::sleep(Duration::new(5, 0));
    for thread_id in 1 .. 10 {
        thread::spawn(move || {
            {
                for i in 1..10 {
                    // any redis clint
                    let mut tcp = TcpStream::connect("127.0.0.1:6379").unwrap();
                    let data: Vec<u8> = vec![i + 49; 26748528];
                    let _ = tcp.write_all(&data);
                    std::thread::sleep(Duration::new(5, 0));
                }
                
                println!("end thread {}", thread_id);
            }
        });
    }
    println!("end this block");
    // now memory is 237m
    std::thread::sleep(Duration::new(500, 0));
}

So my project offten out of memory by this reason. Why This cache so much memory?
In windows or macos, this memory is reduce when thread close.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`C-bugCategory: This is a bug.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleO-linuxOperating system: LinuxT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions