in reply to Memory and garbage collection

The four points you listed are correct.

When a process dies or exists, all its memory is freed. The same holds true for perl programs, because on the operating system level they are just ordinary programs.

The term "memory leak" refers to the fact that a long running program can use more and more memory, without actually storing more information. This happens when memory is not freed, but the program has no references to that piece of memory.

What happens if it is interrupted?

What do you mean by "interrupted"? The scheduler of a multi tasking operation system interrupts your program many times each second, and resumes it afterwards. But it abstracts that away, the process doesn't "feel" that it has been interrupted.

Replies are listed 'Best First'.
Re^2: Memory and garbage collection
by Anonymous Monk on Jul 30, 2008 at 13:24 UTC

    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?

      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.

      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.