2

A process is terminated successfully or abnormally on some OS, when does an OS decide to wipe out the memory (data, code etc.) allocated to that process; at exit or when it wants to allocate memory to a new process?

And is this wiping out memory allocation procedure the same on all operating systems (winXP, Win7, linux, Mac)?

I understand, page table has mapping of virtual addresses for that process and actual physical addresses in the memory.

Thanks.

Junaid
  • 1,668
  • 7
  • 30
  • 51

2 Answers2

5

How an OS reclaims process resources can (and generally does) vary by OS. On the Windows side of things, the NT-derived OSes behave similarly, so there should be little difference between win XP and win7. Note that asking about 'memory' is an over-simplification in this context, because there are different types of memory. For example, a typical Windows application will have stack memory, heap memory (sometimes multiple heaps), instruction/static memory, and perhaps shared memory. Most of this memory is solely owned by the process, and Windows will reclaim it on process termination (even abnormal termination).

However, shared memory can (and often does) have multiple owners; it is tied to a Windows handle (a kernel-level object that can potentially be referenced from multiple processes). Handles have a reference count, and the associated resource is reclaimed if the reference count goes to zero. This means that shared memory can outlive a process that references it. Also, it is possible for a process to 'leak' a handle, and for the handle to never be reclaimed. It is the programmer's responsibility to make sure such handles are properly closed and don't leak; the possibility of abnormal termination complicates this responsibility.

On a side note, when Windows 'reclaims' memory, it simply means that memory is available for future allocations to other processes, etc. The actual 1s and 0s are usually going to sit there up until the OS allocates the memory and the new owner of the memory actively over-writes it. So 'reclaiming' doesn't mean the memory is immediately zeroed out or anything like that; scrubbing the memory in this matter is inefficient and often unnecessary. If you are asking out of security concerns, you shouldn't rely on the OS; you'll need to scrub the memory yourself before your process releases it back to the OS.

If you want to know more about how modern Windows OSes handle memory, and don't mind doing some digging, the Windows API documentation on MSDN has a lot of information on the subject, but it is a little bit scattered. Good places to start would probably be Windows Handles, and load/unload library/process calls. Application Programming for Windows (Richter) probably has some decent information on this, if I remember right, but I don't have a copy on hand right now to check.

Hopefully, someone more knowledgeable about Linux internals can address that side of the question. This is OS-specific stuff, so there are likely to be differences. It might be worth noting that pre-NT Windows (e.g. Windows 95,98,etc.) had a completely different model of process memory. Those differences tended to make it harder for the OS to reclaim memory in the case of abnormal termination; some users found a need to restart the OS frequently if they were running unstable applications, in order to clean up the accumulated memory leak.

Community
  • 1
  • 1
WeirdlyCheezy
  • 704
  • 4
  • 4
  • 1
    It seems to me I recall mention of the Windows "idle process" going about and zero-ing memory in its spare time. But in general you are correct. – Brian White Oct 03 '12 at 20:23
  • @BrianWhite I believe you are correct. I recall reading about something like that as well, but years ago, and probably in the context of Windows 2000? I don't know whether they continued to do this in later versions. Sadly, I haven't had much luck googling for it. Anyone have any reference links on this? – WeirdlyCheezy Oct 03 '12 at 21:54
  • I wonder if they still do, too. I think Windows has a call to fetch zero'd memory which is why it would be useful to do that while the system is idle. Of course, doing so will wipe out the CPU's data cache and so cause a performance hit to processes that try for low latency when responding to requests. – Brian White Oct 04 '12 at 13:05
1

In Linux the resources are normally freed upon termination of the process. You can read about how Linux handles process termination here: http://www.informit.com/articles/article.aspx?p=370047&seqNum=4

There is also the OOM killer which can kick-in in extreme low memory situations I know in the embedded Android world stuff has been happening aorund this but I haven't really kept on top of it LWN.net probably has some coverage.

Matt
  • 231
  • 1
  • 2
  • That's a good read, thanks for the link. One question: The article seems to imply that shared memory is 'all or nothing' for a given process' address space (see bullet on mm_struct). Is this accurate or am I misreading the article? If this is the case, it would be a major difference relative to the Windows API world (where multiple handles to multiple blocks of shared memory can be requested). – WeirdlyCheezy Oct 04 '12 at 18:22
  • I'm not sure I understand what you are asking but shared memory is explicity designed for IPC so other processes can of course access it. See for instance the man page for shmat, which is part of System V shared memory IPC and will attach the designated memory segment to the processes address space. http://linux.die.net/man/2/shmat There is also the map shared option for MMAP, which is widely used for accessing shared memory as well. I hope that clarifies things. – Matt Oct 04 '12 at 23:20
  • My question was due to mis-parsing this quote "If no other process is using this address space (in other words, if it is not shared)" from the article. For some reason I took that to mean that the address space as a whole is either shared, or not shared (rather than having shared segments). I found this rather surprising. From your other link it is clear that this is not actually the case, and I just made an unfortunate choice when parsing that sentence. Thanks for the clarification! – WeirdlyCheezy Oct 05 '12 at 00:15