in reply to Re: Memory and garbage collection
in thread Memory and garbage collection

Oh, sorry, I meant killed, not interrupted. But I guess that brings me back to the first thing you said ("When a process dies [...]").

I'm working on a 64-bit linux machine, and I tried watching what happens to the used memory before, after and during the run of my program. After the program exits, the amount of used memory goes down, but not to what it was before I ran the program (and my process is the only one on that machine that uses a lot of memory....). So now, what I don't understand is - if the OS frees all the program's memory - why does it happen?

Replies are listed 'Best First'.
Re^3: Memory and garbage collection
by moritz (Cardinal) on Jul 30, 2008 at 13:30 UTC
    Linux has the philosophy "if you need free memory, put it beside your PC". Or in other words, it buffers as much as it can, and doesn't show the buffered as "free" just for the sake of having free memory available.

    For example it caches all files read from disk, like your script, the perl interpreter, modules and other data files. So after you run a perl program linux will say it has less free memory. But if you need the memory, it clears the caches and makes that memory available.

    So after running a perl program, or any kind of program, you can have less free memory than before, but you have no loss whatsoever.

    Try running the same program 20 times in row. Memory usage shouldn't increase for each run, just for the first one.

Re^3: Memory and garbage collection
by jethro (Monsignor) on Jul 30, 2008 at 13:45 UTC
    If your memory is less, there are a few possibilities:

    a) your program uses a GUI. X11 might allocate memory for your program which it isn't giving back

    b) your program doesn't really exit. Check your system for defunct processes

    c) some OS caches might fill up memory. Did you for example access the file system with your script?

    d) your OS or some other process interacting with your script might have a memory leak

    You might save the output of 'ps -elf' to some file between a few starts of your script and see if you can spot any differences in the memory footprint of the processes. If not, either the differences are too small or your OS is using up the memory.