你有垃圾收集器的内存泄漏吗? [英] Can you have memory leaks with a garbage collector?

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

问题描述

如果我有一个垃圾收集器跟踪每个分配的对象,并且一旦它们不再有可用的引用就可以释放它们,但是仍然有内存泄漏?



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

编辑:所以我作为内存泄漏计数是分配你在代码中不再有任何参考。大量积累的分配,你仍然有参考不是我在这里考虑的泄漏。



我也只是谈论正常的艺术GC这已经有一段时间了,但我知道像循环引用这样的情况并不会使他们行动起来。我不需要针对任何语言的特定答案,这只是来自我与朋友进行的对话。我们在讨论Actionscript和Java,但我不在乎那些特定的答案。



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

解决方案

如果你的问题真的是这样:

lockquote

考虑到内存泄漏是没有任何引用的分配isn' t
不可能或我缺少什么?

然后答案是是的,那是不可能的,因为正确实施垃圾收集器将回收所有没有活动引用的分配。



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


ObjectA - > ObjectB


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



听起来不是一个大问题,但有些情况下这些泄漏是累积的。我们假设ObjectA和ObjectB实际上是同一个类的实例。而这个程序员忘记将引用设置为null的问题每当使用这种实例时都会发生。最终你会得到如下结果:


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

现在ObjectB到ObjectH都被泄漏了。像这样的问题会(最终)导致你的程序崩溃。即使有一个正确实施的垃圾收集器。


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?

Edit: 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.

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: 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.

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:

ObjectA -> 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".

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 -> etc...

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天全站免登陆