简体   繁体   English

操作系统何时清除进程的内存

[英]when does an operating system wipes out memory of a process

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; 当某个操作系统决定擦除分配给该进程的内存(数据,代码等)时,该进程在某些OS上成功终止或异常终止; 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)? 在所有操作系统(winXP,Win7,linux,Mac)上,这种清除内存分配过程是否相同?

I understand, page table has mapping of virtual addresses for that process and actual physical addresses in the memory. 我了解,页表具有该进程的虚拟地址与内存中实际物理地址的映射。

Thanks. 谢谢。

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. 在Windows方面,NT派生的OS的行为类似,因此win XP和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. 例如,典型的Windows应用程序将具有堆栈内存,堆内存(有时为多个堆),指令/静态内存,以及共享内存。 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). 它与Windows句柄 (一个可以从多个进程引用的内核级对象)绑定在一起。 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. 附带说明一下,当Windows“回收”内存时,它仅表示内存可用于将来分配给其他进程等。在操作系统分配内存新所有者之前,实际的1和0通常会放在那儿。的内存会主动覆盖它。 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. 如果您想了解有关现代Windows操作系统如何处理内存的更多信息,并且不介意进行任何挖掘,则MSDN上的Windows API文档中有很多有关此主题的信息,但有点分散。 Good places to start would probably be Windows Handles, and load/unload library/process calls. Windows Handles和加载/卸载库/进程调用可能是一个不错的起点。 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. 如果我没记错的话,Windows应用程序编程(Richter)可能对此有一些不错的信息,但是我现在没有手头可以检查。

Hopefully, someone more knowledgeable about Linux internals can address that side of the question. 希望有人对Linux内部有更深入的了解可以解决这个问题。 This is OS-specific stuff, so there are likely to be differences. 这是特定于操作系统的内容,因此可能会有差异。 It might be worth noting that pre-NT Windows (eg Windows 95,98,etc.) had a completely different model of process memory. 可能值得注意的是,NT之前的Windows(例如Windows 95、98等)具有完全不同的进程内存模型。 Those differences tended to make it harder for the OS to reclaim memory in the case of abnormal termination; 这些差异往往会导致OS在异常终止的情况下更难回收内存。 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. 一些用户发现如果他们运行的是不稳定的应用程序,则需要经常重新启动OS,以清除累积的内存泄漏。

In Linux the resources are normally freed upon termination of the process. 在Linux中,通常在进程终止时释放资源。 You can read about how Linux handles process termination here: http://www.informit.com/articles/article.aspx?p=370047&seqNum=4 您可以在此处阅读有关Linux如何处理进程终止的信息: 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. 还有一个OOM杀手,它可以在极端的低内存情况下启动,我知道在嵌入式Android世界中,这种情况一直在发生,但是我还没有真正掌握到它,LWN.net可能涵盖了一些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 64 位操作系统上,32 位进程可以访问多少内存? - How much memory can a 32 bit process access on a 64 bit operating system? 从操作系统内核访问GPU内存 - Accessing GPU Memory from Operating System Kernel steady_clock 的纪元是相对于操作系统启动的时间吗? 还是过程本身? - Is the epoch of steady_clock relative to when the operating system starts? or to the process itself? 系统如何定义进程获取的虚拟内存部分? - How does the system define the portion of virtual memory a process gets? 浮动大小是否取决于操作系统? - Does float size depend on operating system? 当VirtualAlloc中的lpAddress为null时,操作系统如何确定要分配的区域 - How does the operating system determine which region to allocate when lpAddress is null in VirtualAlloc 操作系统如何调用用户计划的任务来执行? - How does operating system invoke user-scheduled task that to execute when its time comes to? 当没有GPU或监视器连接到PC时,操作系统的显示缓冲区是否仍然存在? - Does the display buffer of an operating system still exist when there is no GPU or monitor attached to a PC? 如何挂钩我自己进程的所有操作系统调用? - How to hook all operating system calls of my own process? 操作系统如何引入C语言编写内核 - How Does An Operating System Introduces The C Language To Write The Kernel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM