简体   繁体   English

在用C ++编写的进程的出口处堆内存和操作系统

[英]heap memory and OS at the exit of a process written in C++

I have a doubt about the role of the operating system in regards to a process lifetime right now. 我对操作系统在流程生命周期中的作用存在疑问。 I am using Linux. 我正在使用Linux。

Suppose that I have an application that creates a set of objects in the heap using new . 假设我有一个应用程序,它使用new在堆中创建一组对象。 During the lifetime of the application I have no need to delete any of those objects except at the exit the application or on an exception before exiting to do the clean-up. 在应用程序的生命周期中,我不需要删除任何这些对象,除了退出应用程序或异常,然后退出进行清理。

Suppose I don't call delete at the end of the application for all those objects, do usually the OS reclaim/free all the heap allocated to make it available again at the process exits? 假设我没有在应用程序的末尾为所有这些对象调用delete,通常操作系统会回收/释放所有分配的堆,以便在进程退出时再次使用它吗? If the process exit because of an exception or call to return or exit, does this always occurs? 如果进程因异常或调用返回或退出而退出,是否总会发生这种情况?

If this is true this means that if I don't call delete there won't be any impact on the OS or on the other applications running on a machine. 如果这是真的,这意味着如果我不调用delete ,则不会对操作系统或机器上运行的其他应用程序产生任何影响。 Right? 对?

I usually use boost shared pointers or use delete but I would like just to clarify this doubt in a OS/Linux context 我通常使用boost shared pointers或使用delete但我想在OS / Linux上下文中澄清这个疑问

Kind Regards AFG 亲切的问候AFG

That is correct. 那是对的。 Any leak of memory after the lifetime of a process on a protected mode operating system is as a really nasty bug in the kernel (sometimes processes crash). 在受保护模式操作系统上的进程生命周期之后的任何内存泄漏都是内核中一个非常讨厌的错误(有时会发生崩溃)。

Having said that, the simplest way to check for memory leaks is to ensure that the heap has exactly the same number of allocated cells at the end of execution as it has at the beginning of execution. 话虽如此,检查内存泄漏的最简单方法是确保堆在执行结束时具有与执行开始时完全相同的已分配单元数。 If you do not delete on exit, you have no way of checking this and will never discover legitimate memory leaks. 如果在退出时没有delete ,则无法检查此内容,也永远不会发现合法的内存泄漏。

Modern OSes reclaim all resources of a closed process. 现代操作系统回收了封闭过程的所有资源。 Not only memory, but also file handles, etc. 不仅是内存,还有文件句柄等。

No fear, the OS reclaims all the memory. 没有恐惧,操作系统回收所有的记忆。 What you need to watch out for is leaving some persistent resources, such as files, in an indeterminate state. 您需要注意的是将一些持久性资源(例如文件)留在不确定状态。

Just FYI my programming language deliberately fails to free memory on exit by default: it's faster that way. 仅供参考我的编程语言在默认情况下故意无法释放内存:它的速度更快。 However RAII is not permitted. 但是不允许使用RAII。 In C++ if you use RAII then you need to take more care. 在C ++中,如果您使用RAII,那么您需要更加小心。

Fast answer is no damage for OS if program don't call destructors of created objects or not freeing memory or other OS handles. 如果程序不调用已创建对象的析构函数或不释放内存或其他OS句柄,则快速回答不会损坏OS。 Maximum impact is lost part of files which program has written before exiting. 程序在退出之前编写的文件的一部分会丢失最大的影响。

Therefore Herb Satter in their book write about technique that short-lived applications specially doesn't free memory and don't call destructors for maximum speed of execution. 因此,Herb Satter在他们的书中写了关于技术,即短命的应用程序特别不释放内存,并且不会调用析构函数以获得最大的执行速度。

But better that programs normally handles their used OS resources. 但是,程序通常会处理其使用的OS资源。

(Sorry for my English) (对不起我的英语不好)

您需要更加小心其他资源,如文件句柄,数据库连接等。但是,所有内存肯定都是回收的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM