垃圾收集器会导致内存泄漏吗? [英] Can you have memory leaks with a garbage collector?

查看:15
本文介绍了垃圾收集器会导致内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个垃圾收集器来跟踪每个分配的对象并在它们不再有可用的引用时立即释放它们,你还会有内存泄漏吗?

If I have a garbage collector that tracks every object allocated and deallocates them as soon as they no longer have usable references to them can you still have a memory leak?

考虑到内存泄漏是没有任何引用的分配是不可能的还是我错过了什么?

Considering a memory leak is allocations without any reference isn't that impossible or am I missing something?

所以我算作内存泄漏的是你在代码中不再有任何引用的分配.您仍然引用的大量累积分配不是我在这里考虑的泄漏.

So what I'm counting as a memory leak is allocations which you no longer have any reference to in the code. Large numbers of accumulating allocations which you still have references to aren't the leaks I'm considering here.

我也只是在谈论最先进的 G.C.,已经有一段时间了,但我知道像循环引用这样的情况不会绊倒它们.我不需要任何语言的特定答案,这只是来自我与朋友的对话.我们讨论的是 Actionscript 和 Java,但我不在乎这些特定的答案.

I'm also only talking about normal state of the art G.C., It's been a while but I know cases like cyclical references don't trip them up. I don't need a specific answer for any language, this is just coming from a conversation I was having with a friend. We were talking about Actionscript and Java but I don't care for answers specific to those.

Edit2:从它的声音来看,似乎没有任何原因代码可以完全失去引用分配的能力并且没有 GC 能够捡起它,但我仍在等待更多权衡.

From the sounds of it, there doesn't seem to be any reason code can completely lose the ability to reference an allocation and not have a GC be able to pick it up, but I'm still waiting for more to weigh in.

推荐答案

如果您的问题确实是这样的:

If your question is really this:

考虑到内存泄漏是没有任何引用的分配不是那是不可能的还是我错过了什么?

Considering a memory leak is allocations without any reference isn't that impossible or am I missing something?

那么答案是是的,这是不可能的",因为正确实现的垃圾收集器将回收所有没有活动引用的分配.

Then the answer is "yes, that's impossible" because a properly implemented garbage collector will reclaim all allocations that don't have active references.

但是,您绝对可以在(例如)Java 中出现内存泄漏".我对内存泄漏"的定义是仍然具有活动引用的分配(因此它不会被垃圾收集器回收)但是程序员没有'不知道该对象是不可回收的(即:对于程序员来说,这个对象已经死了,应该被回收).一个简单的例子是这样的:

However, you can definitely have a "memory leak" in (for example) Java. My definition of a "memory leak" is an allocation that still has an active reference (so that it won't be reclaimed by the garbage collector) but the programmer doesn't know that the object isn't reclaimable (ie: for the programmer, this object is dead and should be reclaimed). A simple example is something like this:

对象A -> 对象B

在此示例中,ObjectA 是代码中正在使用的对象.但是,ObjectA 包含对 ObjectB 的引用,该引用实际上已死(即:ObjectB 已被分配和使用,现在从程序员的角度来看,已死)但程序员忘记将 ObjectA 中的引用设置为 null.在这种情况下,ObjectB 已经泄露"了.

In this example, ObjectA is an object in active use in the code. However, ObjectA contains a reference to ObjectB that is effectively dead (ie: ObjectB has been allocated and used and is now, from the programmer's perspective, dead) but the programmer forgot to set the reference in ObjectA to null. In this case, ObjectB has been "leaked".

听起来不是什么大问题,但在某些情况下,这些泄漏是累积的.让我们想象一下 ObjectA 和 ObjectB 实际上是同一个类的实例.而这个程序员忘记将引用设置为null的问题,每次使用这样的实例都会发生.最终你会得到这样的结果:

Doesn't sound like a big problem, but there are situations where these leaks are cumulative. Let's imagine that ObjectA and ObjectB are actually instances of the same class. And this problem that the programmer forgot to set the reference to null happens every time such an instance is used. Eventually you end up with something like this:

ObjectA -> ObjectB -> ObjectC -> ObjectD -> ObjectE -> ObjectF -> ObjectG -> ObjectH -> 等等...

ObjectA -> ObjectB -> ObjectC -> ObjectD -> ObjectE -> ObjectF -> ObjectG -> ObjectH -> etc...

现在ObjectB到ObjectH都泄露了.像这样的问题(最终)会导致您的程序崩溃.即使使用正确实施的垃圾收集器.

Now ObjectB through ObjectH are all leaked. And problems like this will (eventually) cause your program to crash. Even with a properly implemented garbage collector.

这篇关于垃圾收集器会导致内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆