Java垃圾收集器澄清 [英] Java Garbage Collector clarification

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

问题描述

现在我正在阅读关于Java垃圾收集的这篇文章: http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html

Right now I'm reading this article regarding Java Garbage Collection: http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html?

这里是JMS客户端中函数的一个片段

Here is a snippet of a function in a JMS client

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

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

这个问题不是关于JMS,但更多的是在foo()函数调用结束后,对象tp发生了什么。函数结束后,无法再引用tp。我假设在createDurableSubscriber()中使用关键字new,这意味着对象被放置在JVM堆上。然而,由于tp不能再被引用,因此它受制于JVM垃圾收集?

This question isn't about JMS but more what happens with the object "tp" after foo() function call has ended. After the function ends there is no way to reference tp anymore. I'm assuming in createDurableSubscriber() that it's using the keyword "new" which means that the object is being placed on the JVM heap. However since tp can no longer be referenced is it subject to the JVM garbage collection?

推荐答案

您需要查看源代码对于 session.createDurableSubcriber()来查看它是否不存储它将返回给你的值。

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.

记住,你基本上是得到一个指向对象的指针(称为Java引用),而不是对象本身,并且即使只有一个对象,该指针也可以存储在许多地方。所有这些指针引用必须在垃圾收集器回收对象之前完成。

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. All these pointer references must be done with before the object can be reclaimed by the garbage collector.

这篇关于Java垃圾收集器澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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