简体   繁体   English

为什么需要显式删除动态分配的 memory?

[英]Why the need to explicitly delete dynamically allocated memory?

Why do we need to explicitly delete dynamically allocated memory, when the OS itself takes back memory of process?为什么我们需要显式删除动态分配的memory,当OS自己收回进程的memory? In the kind of situation where dynamic memory is allocated inside a constructor, why do we need to delete it in the destructor?在构造函数内部分配动态 memory 的情况下,为什么我们需要在析构函数中删除它?

To some extent, you do not have to explicitly delete allocated memory.在某种程度上,您不必显式删除分配的 memory。 You can let memory leaks accumulate until your program ends and the OS reclaims the memory for you.可以让 memory 泄漏累积,直到您的程序结束并且操作系统为您回收 memory。 A better question is do you want to?一个更好的问题是你想要吗?

The OS will not reclaim the memory until your process ends.在您的进程结束之前,操作系统不会回收 memory。 Can you afford to wait that long?你能等那么久吗? If you allocate less than a megabyte of memory over the course of your program, you are likely to not see bad side-effects.如果您在程序执行过程中分配的 memory 少于 1 兆字节,您可能不会看到不良的副作用。 However, if your program could be run for hours, or even days, how much memory is then leaked?但是,如果您的程序可以运行数小时甚至数天,那么会泄露多少 memory? Eventually you get to the point where other programs are slowed down because your program is hogging all the memory – and it's not even using that memory;最终你会因为你的程序占用所有 memory 而导致其他程序变慢 - 而且它甚至没有使用 memory; the leaked memory is floating around unused and unusable.泄漏的 memory 在未使用和不可用的周围浮动。

If you learn to clean up after yourself while your programs are small, you'll be prepared to tackle larger projects that can potentially leak all available memory on a system.如果您在程序很小的时候学会自己清理,您将准备好处理可能会泄漏系统上所有可用 memory 的大型项目。 Also, it's polite to not take more than what you need (or what you've reasonably estimated as needed) of a limited resource.此外,在有限的资源中,不要占用超过您需要(或您合理估计的需要)的资源,这是有礼貌的。

why we need to explicitly delete dynamically allocated memory, when OS itself takes back memory of process为什么我们需要显式删除动态分配的memory,当操作系统本身收回进程的memory

Because we might want the process to keep running.因为我们可能希望进程继续运行。 If you allocate a megabyte every second and don't deallocate any memory, then after a day of running the process, you've allocated 84 gigabytes.如果您每秒分配 1 MB 并且不释放任何 memory,那么在运行该进程一天后,您已分配 84 GB。 At some point you will run out of memory.在某些时候,您将用完 memory。 It would be highly inconvenient if we had to restart all programs every few hours just to give operating system some memory back.如果我们不得不每隔几个小时重新启动所有程序只是为了给操作系统一些 memory 回来,那将是非常不方便的。

Yes, the OS will clean-up after you, but there are 2 major problems:是的,操作系统会在你之后进行清理,但有两个主要问题:

  1. Destructors of leaked objects will not be called.不会调用泄漏对象的析构函数。
  2. Your program can run out of memory (and other resources it leaks) WHILE it's running.您的程序在运行时可能会用完 memory(以及它泄漏的其他资源)。

Having said that, you can use smart pointers and STL containers to manage memory and froget about it.话虽如此,您可以使用智能指针和 STL 容器来管理 memory 和青蛙。

The OS recycle of memory will only happen after/during the termination of the program. memory 的操作系统回收只会在程序终止之后/期间发生。

If you are writing a program that persists for a long time, such as a background daemon running as long as the PC is on, due to unfreed memory created during the operation, your PC's memory resource will eventually get used up, triggering the Low Memory Killer of your OS, which kills some programs (which may include yours) and triggers the recycling until the system has enough memory again. If you are writing a program that persists for a long time, such as a background daemon running as long as the PC is on, due to unfreed memory created during the operation, your PC's memory resource will eventually get used up, triggering the Low Memory您的操作系统的杀手,它会杀死一些程序(可能包括您的程序)并触发回收,直到系统再次有足够的 memory。 From a stability perspective you want to avoid this as much as possible.从稳定性的角度来看,您希望尽可能避免这种情况。

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

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