内存泄漏 - 缺少垃圾收集器
让我们考虑一个内存泄漏程序,其中一块堆内存没有被释放并且程序终止。如果这是(比如说)一个 Java 程序,那么内置的垃圾收集器将在程序退出之前自动释放该堆块。
但即使在 C++ 中,如果程序退出,内核也不会自动取消分配与进程关联的所有空间。同样在 Java 代码中,内核必须为进程的文本部分(代码)取消分配空间(即使堆栈和堆部分由垃圾收集器取消分配)。那么,使用垃圾收集器功能的总体优势是——只是增加了程序本身而不是内核释放堆所需的时间? (如果有任何这样的节省)
编辑:我的一个主要疑问是查看响应 - 当内存使用达到限制时,GC 会自动调用自身吗?因为,如果仅在程序终止之前调用 GC,那么它对于长程序没有用处。
Let us think of a memory leak program, wherein a block of heap memory is not freed and the program terminates. If this was (say) a Java Program, the in-built garbage collector would have automatically deallocated this heap block before the program exits.
But even in C++, if the program exits, wouldn't the Kernel automatically de-allocate all space associated with the process. Also in the Java code, the kernel would have to de-allocate the space for the text part (code) of the process (even if the stack and heap parts are deallocated by the garbage collector). So is the overall advantage of using a garbage collector feature - just the increased savings in time required to deallocate the heap by the program itself rather than the kernel? (if there is any such savings)
EDIT: A primary doubt of mine that has come about looking at the responses - will the GC invoke itself automatically when memory usage reaches a limit? Because, if a GC is only invoked just before the program terminates, it is not going to be useful for long programs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以不,GC 并不是为了节省时间。它可以节省大量内存,防止长时间运行的分配密集型程序占用所有可用内存,并最终导致每个人都因内存不足错误而死亡。
So no, GC isn't about saving time. It's about saving tons of memory, preventing long-running allocation-intense programs from hogging all available memory and eventually making everyone die from out of memory errors.
假设程序分配了一些资源,在运行时一直使用它,但在退出之前没有正确释放它。当该程序退出时,内核会释放所有程序资源 - 这是可以的。
现在考虑这样的情况:某个函数在每次调用时都会产生内存泄漏,并且该函数每秒被调用 100 次。几分钟或几小时后,该程序因没有可用内存而崩溃。
糟糕的是,造成类型 1 内存和资源泄漏的程序员通常会造成类型 2 泄漏,从而产生肮脏且不稳定的代码。专业程序员编写完美的代码,0资源和内存泄漏。如果垃圾收集器可用 - 就可以了。如果没有 - 自己管理资源。
顺便说一句,垃圾收集器仍然有可能造成泄漏 - 就像众所周知的 .NET 事件源-消费者泄漏一样。所以,垃圾收集器非常有用,节省了开发人员大量的时间,但无论如何开发人员必须小心管理程序资源。
Let's say that program allocates some resource, uses it all time it is running, but doesn't release it properly before exit. When this program exits, kernel deallocates all program resources - it is OK.
Now consider situation when some function creates memory leak on every call, and this function is called 100 times in a second. After few minutes or hours this program crashes because there is no free memory.
The bad thing is that programmer who makes memory and resource leaks of type 1, usually makes a leaks of type 2, producing dirty and unstable code. Professional programmer writes perfect code with 0 resource and memory leaks. If garbage collector is available - it is OK. If not - manage resources yourself.
BTW, it is still possible to make a leaks with garbage collector - like well-known .NET event source-consumer leak. So, garbage collector is very useful, saves a lot of developer time, but in any case developer must carefully manage program resources.