简体   繁体   English

已分配但未释放的内存

[英]Memory that is malloc'ed and not freed

I have read this- 我读过这个

Memory that is allocated by malloc(for example) and that is not freed using free() function is released when the program terminates.And that it is done by the opearting system. 程序终止时,将释放由malloc(例如)分配的,但未使用free()函数释放的内存,并由运行系统完成。 So when does having or not having a garbage collector come into picture? 那么什么时候有了垃圾收集器呢?

Or is it that not all operating systems do this automatic release of memory on program termination? 还是不是所有操作系统都在程序终止时自动释放内存?

That claim about malloc and free is correct for all modern computing operating systems. 关于mallocfree主张对于所有现代计算操作系统都是正确的。 But the statement as a whole reflects a complete misunderstanding of the purpose of garbage collection. 但是,该声明从整体上反映了对垃圾回收目的的完全误解。

The reason you call free is not to clean things up for after your program terminates. 调用free的原因不是在程序终止后清理所有内容。 The reason you call free is to permit the memory to be re-used during the subsequent execution of a long-running program. 调用free的原因是允许在以后运行长时间运行的程序期间重新使用内存。

Consider a message server that handles a hundred messages per second. 考虑一个每秒处理一百条消息的消息服务器。 You call malloc when you receive a new message. 收到新消息时,您将调用malloc And then you have to do lots of things with it. 然后,您必须使用它来做很多事情。 You may have to log it. 您可能需要记录它。 You may have to send it to other clients. 您可能必须将其发送给其他客户端。 You may have to write it to a database. 您可能必须将其写入数据库。 When you are done, if you don't free it, after a few days you'll have millions of messages stuck in memory. 完成后,如果不free它,几天后,您将有数百万条消息保留在内存中。 So you have to call free . 因此,您必须拨打free电话。

But when do you call free ? 但是什么时候free电话? When one client is done sending a message, another client might still be using it. 一个客户端发送完消息后,另一客户端可能仍在使用它。 And maybe the database still needs it. 也许数据库仍然需要它。

The purpose of garbage collection is to ensure that object's used memory is released (so it can be re-used to hold a new message during the application's lifetime) without having to burden the application programmer with the duty (and risks) associated with tracking exactly when the object is no longer required by any code that might be using it. 垃圾回收的目的是确保释放对象的已用内存(以便可以在应用程序的生命周期内重新使用该对象以保存新消息),而不必使应用程序程序员承担与准确跟踪有关的职责(和风险)当可能使用该对象的任何代码不再需要该对象时。

If an application doesn't run for very long or doesn't have any objects whose lifetimes are difficult to figure out, then garbage collection doesn't do very much good. 如果一个应用程序不能运行很长时间或者没有任何对象的生命周期很难弄清楚,那么垃圾回收就没有太大的用处。 And there are other techniques (such as reference-counted pointers) that can provide many of the same benefits as garbage collection. 还有其他技术(例如引用计数的指针)可以提供与垃圾回收相同的许多好处。 But there is a real problem that garbage collection does solve. 但是,垃圾回收确实解决了一个真正的问题。

Most modern operating systems will indeed free everything you're allocated on program termination. 实际上,大多数现代操作系统都会释放您在程序终止时分配的所有内容。 However, a garbage collector will free unused memory before program termination. 但是,垃圾回收器将程序终止之前释放未使用的内存。 This allows your program to skip the frees, but still manage to keep allocating memory indefinitely, as long as it lets go to references to memory that isn't being used anymore, and as long as your total working set size doesn't exceed physical memory limits. 这使您的程序可以跳过空闲时间,但仍设法无限期地分配内存,只要它允许转到对不再使用的内存的引用,并且只要您的总工作集大小不超过物理内存即可内存限制。

All OS do free memory when the program quits. 程序退出时,所有操作系统都会释放可用内存。 Memory leaks are 'only' a problem because they waste memory on the machine, or cause the program to crash. 内存泄漏仅是一个问题,因为它们浪费了计算机上的内存,或导致程序崩溃。 So you'd garbage collect to prevent these things from happening, but without worrying about actually freeing your own pointers when you're done with them. 因此,您将进行垃圾收集以防止这些事情发生,但是不必担心在完成使用它们后实际上会释放自己的指针。

They're two solutions to the same problem, really. 实际上,它们是解决同一问题的两个解决方案。 But, it's because the problem happens during runtime that you're worried about it. 但是,这是因为问题在运行时发生,您对此感到担心。

Imagine a long running process like a web server that malloc() sa bunch of data structures for every connection it services. 想象一个像Web服务器这样的长期运行的过程,它为服务的每个连接malloc()一堆数据结构。 If it never free() s any of that memory, the process memory usage will continually grow, possibly consuming everything available on the system. 如果它从不free()任何内存,进程内存使用量将不断增长,可能会消耗系统上的所有可用资源。

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

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