简体   繁体   English

Java垃圾收集器说明

[英]Java Garbage Collector clarification

Right now I'm reading this article regarding Java Garbage Collection: http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html ? 现在,我正在阅读有关Java垃圾收集的文章: http : //www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html

Here is a snippet of a function in a JMS client 这是JMS客户端中的功能片段

public void foo(){
    ...//Create Connection factory, connection, and session, topic

    TopicSubscriber tp = session.createDurableSubcriber(topic,"001");
    tp.setMessageListener(this)
}

This question isn't about JMS but more what happens with the object "tp" after foo() function call has ended. 这个问题不关乎JMS,而是更多关于foo()函数调用结束后对象“ tp”会发生什么。 After the function ends there is no way to reference tp anymore. 函数结束后,将无法再引用tp。 I'm assuming in createDurableSubscriber() that it's using the keyword "new" which means that the object is being placed on the JVM heap. 我假设在createDurableSubscriber()中使用的是关键字“ new”,这意味着该对象将放置在JVM堆上。 However since tp can no longer be referenced is it subject to the JVM garbage collection? 但是,由于无法再引用tp,它是否受JVM垃圾回收的约束?

You need to look in the source code for session.createDurableSubcriber() to see if it doesn't store the value it will return to you somewhere. 您需要在源代码中查找session.createDurableSubcriber()以查看它是否不存储将返回给您的值。

Remember you are basically getting a pointer (called reference in Java) to the object, not the object itself, and that pointer can be stored numerous places even if you only have a single object. 请记住,您基本上是在获得指向对象的指针(在Java中称为引用),而不是对象本身,并且即使您只有一个对象,该指针也可以存储在很多地方。 All these pointer references must be done with before the object can be reclaimed by the garbage collector. 必须先完成所有这些指针引用,然后垃圾回收器才能回收该对象。

Possibly. 可能吧。 It might still be referenced through some chain of pointers starting at a static variable somewhere. 仍然可以通过从某个地方的静态变量开始的一些指针链来引用它。

It could be referenced in your session as a field or passed anywhere else depending on the JMS implementation. 根据JMS的实现,它可以在您的会话中作为字段引用,也可以传递到其他任何地方。 JMS is only an API, you simply can't suppose anything about the implementation, and you can't suppose that tp isn't referenced anymore. JMS只是一个API,您根本无法假设有关实现的任何内容,也不能假设不再引用tp

But to answer the question, if you "suppose" anyway that it isn't referenced, yes the GC would take care of it. 但是要回答这个问题,如果无论如何“假设”未引用它,是的,GC会处理它。

An object will only be collected if no running code has a reference to it (excluding weak references, which few people generally mess with anyway -- weak references don't count for determining collectibility.). 仅当没有运行代码对其引用时才收集对象(不包括弱引用,通常很少有人惹恼对象-弱引用不影响确定可收集性)。

In your example, if you assume that create... actually creates a new object, and doesn't store a reference to it for some reason, and that attaching a listener to said object doesn't require creating a link back to the observable, then yes -- tp will probably be eligible for finalization and collection. 在您的示例中,如果您假设create...实际上创建了一个新对象,并且由于某种原因未存储对它的引用,并且将侦听器附加到所述对象上并不需要创建指向可观察对象的链接,然后是tp可能有资格完成和收集。

If any of those assumptions are wrong, though, all bets are off. 但是,如果这些假设中的任何一个错误,所有的赌注都将被取消。

It is important to distinguish between objects and variables (which holds references to objects). 区分对象和变量(保留对对象的引用)很重要。 An object becomes eligible for garbage collection when there are no more references to it. 如果没有更多引用,则该对象可以进行垃圾回收。

In you particular case, createDurableSubscriber will have kept a copy of the reference it returned, thereby preventing the object from being collected. 在您的特定情况下, createDurableSubscriber将保留它返回的引用的副本,从而防止收集对象。 (After all, it needs to invoke methods on that object when a new message arrives, which is hard to do without a reference), (毕竟,它需要在收到新消息时在该对象上调用方法,这很难在没有引用的情况下完成),

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

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