操作系统何时清除进程的内存 [英] when does an operating system wipes out memory of a process

查看:68
本文介绍了操作系统何时清除进程的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某个进程在某些操作系统上成功或异常终止,操作系统何时决定清除分配给该进程的内存(数据、代码等);在退出时或当它想为新进程分配内存时?

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?

在所有操作系统(winXP、Win7、linux、Mac)上,这种清除内存分配的过程是否都一样?

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.

谢谢.

推荐答案

操作系统回收进程资源的方式可以(并且通常会)因操作系统而异.在 Windows 方面,NT 派生的操作系统的行为类似,因此 win XP 和 win7 之间应该没有什么区别.请注意,在这种情况下询问记忆"是一种过度简化,因为有不同类型的记忆.例如,典型的 Windows 应用程序将具有堆栈内存、堆内存(有时是多个堆)、指令/静态内存,可能还有共享内存.大部分内存由进程单独拥有,Windows 会在进程终止(甚至异常终止)时回收它.

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).

然而,共享内存可以(并且经常)有多个所有者;它绑定到 Windows 句柄(一个内核级对象,可能被引用来自多个进程).句柄有一个引用计数,如果引用计数变为零,则回收相关资源.这意味着共享内存可以比引用它的进程存活时间更长.此外,进程可能会泄漏"句柄,并且永远不会回收该句柄.确保这些句柄正确关闭并且不会泄漏是程序员的责任;异常终止的可能性使这项责任变得复杂.

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.

附带说明一下,当 Windows回收"内存时,它只是意味着内存可用于将来分配给其他进程等.实际的 1 和 0 通常会一直放在那里,直到操作系统分配内存 内存的新所有者主动覆盖它.所以回收"并不意味着内存立即清零或类似的东西;在这件事上清理内存效率低下,而且通常是不必要的.如果您出于安全考虑而提出要求,则不应依赖操作系统;在您的进程将其释放回操作系统之前,您需要自己清理内存.

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.

如果您想了解更多关于现代 Windows 操作系统如何处理内存的信息,并且不介意进行一些挖掘,MSDN 上的 Windows API 文档有很多关于该主题的信息,但它有点分散.开始的好地方可能是 Windows 句柄和加载/卸载库/进程调用.Windows 应用程序编程 (Richter) 可能有一些如果我没记错的话,这方面的信息不错,但我现在手头没有副本要检查.

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.

希望对 Linux 内部更了解的人可以解决这个问题.这是特定于操作系统的东西,因此可能存在差异.值得注意的是,NT 之前的 Windows(例如 Windows 95、98 等)具有完全不同的进程内存模型.这些差异往往会使操作系统在异常终止的情况下更难回收内存;一些用户发现,如果运行不稳定的应用程序,需要经常重启操作系统,以清理累积的内存泄漏.

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.

这篇关于操作系统何时清除进程的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆