进程终止会自动释放所有已使用的内存吗?有什么理由明确地这样做吗? [英] Does process termination automatically free all memory used? Any reason to do it explicitly?

查看:291
本文介绍了进程终止会自动释放所有已使用的内存吗?有什么理由明确地这样做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows NT和更高版本中,我假设某个进程到期时(由于它本身已终止或被强制终止),操作系统将自动回收该进程使用的所有内存.在任何情况下都不是这样吗?是否有任何理由明确释放用户模式应用程序使用的所有内存?

In Windows NT and later I assume that when a process expires, either because it terminated itself or was forcefully terminated, the OS automatically reclaims all the memory used by the process. Are there any situations in which this is not true? Is there any reason to free all the memory used by a user-mode application explicitly?

推荐答案

每当进程结束时,映射到该进程的所有内存页面都将返回可用状态.正如您所说,这可以称为回收内存".但是,它不会执行诸如运行析构函数(如果使用的是C ++)之类的事情.

Whenever a process ends, all memory pages mapped to it are returned to the available state. This could qualify as "reclaiming the memory," as you say. However, it does not do things such as running destructors (if you are using C++).

高度建议释放所有内存,而不是从资源的角度,而是从开发的角度.尝试释放内存可以鼓励您考虑内存的寿命,并帮助您确保正确清理内存.

I highly recommend freeing all memory, not from a resources perspective, but from a development perspective. Trying to free up memory encourages you to think about the lifespan of memory, and helps you make sure you actually clean up properly.

这在短期内无关紧要,但是我已经处理了无数软件程序,这些程序假定它们拥有进程,因此不必自己清理.但是,有很多原因要在沙箱中运行程序.如果不必每次都重新创建过程,则许多随机测试方案的运行速度会更快.我也有几个程序认为它们是独立的,只是想找到集成到更大软件包中的愿望.那时,我们发现了内存管理所采用的所有快捷方式.

This does not matter in the short run, but I have dealt with countless software programs which assumed that they owned the process, so didn't have to clean up after themselves. However, there are a lot of reasons to want to run a program in a sandbox. Many randomized testing scenarios can run much faster if they don't have to recreate the process every time. I've also had several programs which thought they would be standalone, only to find a desire to integrate into a larger software package. At those times, we found out all the shortcuts that had been taken with memory management.

这篇关于进程终止会自动释放所有已使用的内存吗?有什么理由明确地这样做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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