Linux和内存泄漏 [英] Linux and memory leaks

查看:231
本文介绍了Linux和内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux是否会立即自动回收应用程序使用的所有内存?

Does Linux automatically re-claim all memory used by an applications immediately?

如果是这样,那么应用程序真的应该费心去退出之前释放所有内存吗?

If so then should the application really bother about freeing all memory before exit?

在调用exit(0)之前真的值得在多线程应用程序中调用每个类的析构函数吗?

Is it really worth to call the destructor of every class in a multi-threading application before making a call to exit(0)?

如果Linux总是立即回收应用程序使用的所有内存,那么内存泄漏只是该应用程序创建的悬挂指针,也仅仅是它的生存期.

If Linux always re-claims all memory used by an application immediately, then memory leaks are only the dangling pointers which the application has created and that too only it its lifetime.

推荐答案

Linux是否会立即自动回收应用程序使用的所有内存?

Does Linux automatically re-claim all memory used by an applications immediately?

否,但从某种意义上讲是肯定的.释放属于该进程的所有虚拟内存.未共享的帧可用于其他进程.

No, but yes in the sense that you're implying. All virtual memory belonging to the process are released. Frames that aren't shared are made available to other processes.

如果是这样,那么应用程序真的应该在退出之前释放所有内存吗?

If so then should the application really bother about freeing all memory before exit?

是的,原因如下:

  • 您将来可能会决定将代码扩展到其他目的,稍后添加清理可能会很困难.
  • 您的内存使用过多,实际上需要浪费"虚拟内存空间.
  • 您需要找出一些错误:不小心释放所获得的资源将使调试变得非常困难.

在某些情况下,如果您不希望释放内存,可能会出现这种情况,通常情况下,这些情况与性能相关,并且仅针对这些情况.

There are situations that could arise when not freeing memory is what you want, generally these will be performance related, and only specific to those situations.

在调用exit(0)之前真的值得在多线程应用程序中调用每个类的析构函数吗?

Is it really worth to call the destructor of every class in a multi-threading application before making a call to exit(0)?

这与上一个问题几乎相同.还要注意,不从第三方释放资源和OS库实际上与不释放内存相同.

This is pretty much the same as the last question. Also note that not releasing resources from third party, and OS libraries is effectively the same as not freeing memory.

如果Linux总是立即回收应用程序使用的所有内存,那么内存泄漏只是该应用程序创建的悬挂指针,也仅仅是它的生存期.

If Linux always re-claims all memory used by an application immediately, then memory leaks are only the dangling pointers which the application has created and that too only it its lifetime.

是的.这种理论唯一一次崩溃的时候是所拥有的资源是全球性的,并且不会在流程终止时消失.共享内存,设计不当的第三方库,临时文件等就是这些示例.

Yup. The only time this theory breaks down is when resources held are global, and don't go away at process termination. Shared memory, poorly designed third party libraries, temporary files etc. are examples of these.

这篇关于Linux和内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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