#is this a memory leak?

74 messages · Page 1 of 1 (latest)

molten crescent
#

im reading and parsing a bunch of .txt files, nothing crazy. no unsafe involved. my program has already exited, but RAM usage is still through the roof, without taskmanager or resource monitor showing which process is holding it (see images). any ideas whats going on here?

pure snow
#

even if you had a memory leak the memory should be freed on program exit

molten crescent
#

yeah thats what confuses me

#

i know i can just reboot and that will most likely fix it, but i'd like to actually figure out whats going on

sand birch
#

Any chance it's just the file cache, meaning it will get evicted as soon as any process needs memory?

molten crescent
#

possible, how do i check?

#

no wait, the files im parsing are <3gb total

pure snow
#

try just running a program that allocates a bunch of memory

#

see what happens

balmy steeple
#

lol sweatremoval

sand birch
molten crescent
#

but how can 1.5gb of files cause 30gb of ram usage?

#

like even if it held literally everything in ram that wouldnt even come close

#

im suspecting rust-analyzer

pure snow
molten crescent
#

will a simple vec![0u8; 10_000_000_000] suffice?

pure snow
#

sure yeah

molten crescent
#

uh

#

i think its getting optimized?

#

this doesnt show up either

pure snow
#

right, well you can try using them

balmy steeple
#

add a sleep() after it and black_box it

molten crescent
#

i tried mutating it with indices i input with stdin

#
fn main() {
    let mut x = vec![0_u8; 10_000_000_000];
    
    loop {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let i = s.trim_end().parse::<usize>().unwrap();
        x[i] = 42;
        
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let i = s.trim_end().parse::<usize>().unwrap();
        println!("{}", x[i]);
    }
}
balmy steeple
#

this doesnt show up, at all?

#

do you have anything like btop

molten crescent
#

dunno what that is, but im on a fresh windows 11 install

#

fresh as in 1 day old

#

closed everything:

#

still over 26gb occupied

#

and the code i posted earlier supposedly uses 300kb only

#

ooh its using virtual memory

sand birch
#

Oh, yeah, you'll have to actually use each memory page in order to have them in RAM

molten crescent
#

how do i do that lol

#

loop over the vec and write to everything?

sand birch
#

A loop would help

molten crescent
#
fn main() {
    let mut x = vec![0_u8; 10_000_000_000];
    
    for i in 0..x.len() {
        x[i] = (i % 256) as _;
    }
    
    loop {
        let i = read_input();
        x[i] = read_input() as _;
        
        let i = read_input();
        println!("{}", x[i]);
    }
}

fn read_input() -> usize {
    let mut buf_string = String::new();

    std::io::stdin()
        .read_line(&mut buf_string)
        .unwrap();
    buf_string
        .trim_end()
        .parse()
        .unwrap()
}
#

now it shows up properly

#

but it seems to struggle to acquire the memory

#

i'd expect to be able to start inputting stuff after a few seconds at most

#

but after a minute it still didnt let me

#

so i assume its constantly swapping pages?

sand birch
#

I mean, it's not surprising your computer would struggle to free up 10 GB of RAM on your whim

molten crescent
#

really? i thought the whole point of RAM is exactly that

sand birch
#

If a process has recently written to RAM, the OS has to write that page to disk before assigning it to you. If you do that for 10 000 000 pages, that takes a while.

molten crescent
#

im not an expert, but i feel like this would kinda defeat the point of having ram in the first place

#

could you try the code yourself and tell me how it performs on your machine?

sand birch
#

Yours isn't the only process that uses RAM.

molten crescent
#

so what

sand birch
#

But anyways, it does appear not all of the 10 GB you decided to take up for yourself was file cache or already commited pages.

#

What you could try is to keep allocating RAM until it takes more than one second to perform a write, and then immediately quit.

molten crescent
#

i've let it run for a while now, and my entire system started lagging

#

until i eventually got this

#

and shouldnt exiting the process free up all the memory?

sand birch
#

It should free up the process's private memory. It may not free up the files that the process read.

molten crescent
#

but again, its just 1.5gb of files, nowhere near enough to explain the 20+ im looking at

sand birch
#

Maybe Windows is caching the target/

#

could be anything

molten crescent
#

my target is 3.2gb

#

idk i find it hard to believe that this is normal

#

RAMMap64 from sysinternals

#

interesting that it says 11gb is unused

#

i dont really know what any of this means, just throwing stuff out here

sand birch
#

Well it seems your memory cleaner did clean your memory...

molten crescent
#

except that task manager still reports 99% usage

sand birch
molten crescent
#

and opening any more programs lags my whole system

#

ok im rebooting now

ruby salmon
molten crescent
ruby salmon
#

You can disregard me then

molten crescent
#

as expected, the problem was gone after the reboot, so i dont think we'll ever figure out what it was

ruby salmon
#

wsl just has the possibility to eat memory that windows can't see