C++ 程序退出时是否释放了泄漏的内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2975831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:38:45  来源:igfitidea点击:

Is leaked memory freed up when the program exits?

c++memory-leaks

提问by Martijn Courteaux

If I programmed — without knowing it — a memory leak, and the application terminates, is the leaked memory freed?

如果我在不知情的情况下编写了内存泄漏,并且应用程序终止,泄漏的内存是否被释放?

回答by Justin Ethier

Yes, a "memory leak" is simply memory that a process no longer has a reference to, and thus can no longer free. The OS still keeps track of all the memory allocated to a process, and will free it when that process terminates.

是的,“内存泄漏”只是进程不再引用的内存,因此不能再释放。操作系统仍会跟踪分配给进程的所有内存,并在该进程终止时将其释放。

In the vast majority of cases the OS will free the memory - as is the case with normal "flavors" of Windows, Linux, Solaris, etc. However it is important to note that in specialized environments such as various Real-Time Operating Systems the memory may not be freed when the program is terminated.

在绝大多数情况下,操作系统会释放内存 - 就像 Windows、Linux、Solaris 等正常“风格”的情况一样。但是,重要的是要注意,在各种实时操作系统等特殊环境中,程序终止时可能不会释放内存。

回答by Brian R. Bondy

The OS executing your program usually does cleanup memory that is not freed explicitly and handles that are not closed explicitly, but this is not guaranteed by the C++ standard. You may find some embedded device that do not free up your memory leaks.

执行程序的操作系统通常会清理未显式释放的内存和未显式关闭的句柄,但 C++ 标准不保证这一点。您可能会发现一些嵌入式设备无法释放内存泄漏。

That being said Windows and all distros of Linux that I've ever seen do free up memory leaks.

话虽如此,我见过的 Windows 和所有 Linux 发行版都可以消除内存泄漏。

You can easily create a huge loop of memory leaks though to test it out yourself. Watch your RAM usage grow and then close your program. You will see that the RAM usage goes back down.

你可以很容易地创建一个巨大的内存泄漏循环来测试它。观察您的 RAM 使用量增长,然后关闭您的程序。您将看到 RAM 使用率回落。



Another consideration when using C++ is that if you aren't deleting your heap allocated memory then your destructors are also not being called. Sometimes you will have other side effects as well if your destructors aren't called.

使用 C++ 时的另一个注意事项是,如果您没有删除堆分配的内存,那么您的析构函数也不会被调用。有时,如果不调用析构函数,还会产生其他副作用。

回答by Vicky

Are you running on a desktop OS (Windows, Linux etc.)? If so, yes, in general the system will free any memory associated with the program when the program exits.

您是否在桌面操作系统(Windows、Linux 等)上运行?如果是这样,是的,通常系统会在程序退出时释放与程序关联的任何内存。

回答by Jerry Coffin

Usually, yes. Some systems support things like shared memory blocks that aren't automatically freed when a program exits though. Most still keep a reference count and delete it when all the programs that opened it exit, but a few don't (e.g., 16-bit Windows had a few types of items that would remain allocated even when nothing referred to them -- though it usually crashed for other reasons before enough of this accumulated to cause a problem...)

通常,是的。一些系统支持诸如共享内存块之类的东西,但在程序退出时不会自动释放它们。大多数仍然保留一个引用计数并在所有打开它的程序退出时将其删除,但有一些没有(例如,16 位 Windows 有几种类型的项目即使没有引用它们也会保持分配状态——尽管它通常会因为其他原因而崩溃,然后积累到足够的数量导致问题......)

回答by ravibhagw

As far as I know, a modern operating system will free this memory once the program terminates.

据我所知,一旦程序终止,现代操作系统将释放此内存。

回答by Puppy

Depends on what memory you leaked. Some memory can't be reclaimed by the OS. Most memory on most OSes however will be automatically reclaimed when the process exits.

取决于您泄漏的内存。操作系统无法回收某些内存。然而,大多数操作系统上的大部分内存会在进程退出时自动回收。