dcn
dcn

Reputation: 4469

Reclaim memory after program exit

Here is my problem: after running a suite of programs, free tells me that after execution there is about 1 GB less memory free. After some searches I found SO: What really happens when you dont free after malloc which (as I understand it) makes clear that missing memory deallocations should not be the problem... (is that correct?)

top does not show any processes that use significant amounts of memory.

How can I find out 'what happend' to the memory, i.e. which program allocated it and why it is not free after program execution?

Where does free collect its information?

(I am running a recent Ubuntu version)

Upvotes: 6

Views: 5870

Answers (3)

Damon
Damon

Reputation: 70186

free (1) is a misnomer, it should more correctly be called unused, because that's what it shows. Or maybe it should be called physicalfree (or, more precisely, the "free" column in the output should be named "unused").
You'll note that "buffers" and "cached" tends to go up as "free" goes down. Memory does not disappear, it just gets assigned to a different "bucket".

The difference between free memory and unused memory is that while both are "free", the unused memory is truly so (no physical memory in use) whereas the simply "free" memory is often moved into the buffer cache. That is for example the case for all executable images and libraries, anything that is read-only or read-execute. If the same file is loaded again later, the "free" page is mapped into the process again and no data must be loaded.

Note that "unused" is actually a bad thing, although it is not immediately obvious (it sounds good, doesn't it?). Free (but physically used) memory serves a purpose, whereas free (unused) memory means you could as well have saved on money for RAM. Therefore, having unused memory (e.g. by purging pages) is exactly what you don't want.
Stunningly, under Windows there exists a lot of "memory optimizer" tools which cost real money and which do just that...

About reclaiming memory, the way this works is easy: The OS simply removes the references to all pages in the working set. If a page is shared with another process, nothing spectacular happens. If it belongs to a non-anonymous mapping and is not writeable (or writeable and not written), it goes into the buffer cache. Otherwise, it goes zap poof.
This removes any memory allocated with malloc as well as the memory used by executables and file mappings, and (since all memory is based on pages) everything else.

Upvotes: 2

paulsm4
paulsm4

Reputation: 121759

Yes, memory used by your program is freed after your program exits.

The statistics in "free" are confusing, but the fact is that the memory IS available to other programs:

http://kevinclosson.wordpress.com/2009/11/17/linux-free-memory-is-it-free-or-reclaimable-yes-when-i-want-free-memory-i-want-free-memory/

http://sourcefrog.net/weblog/software/linux-kernel/free-mem.html

Here's an event better link:

http://www.linuxatemyram.com/

Upvotes: 4

T.E.D.
T.E.D.

Reputation: 44804

It is probably your OS using up that space for its own purposes.

For example, many modern OS's will keep programs loaded in memory after they terminate, in case you want to start them up again. If their guess is right, it saves a lot of time at the cost of some memory that wasn't being used anyway. Some OS's will even speculatively load some commonly used programs.

CPU utilization works the same way. Often your OS will speculatively do some work when the CPU would otherwise be "idle".

Upvotes: 0

Related Questions