phantom-reference

Java: PhantomReference, ReferenceQueue and finalize

久未见 提交于 2019-12-01 03:46:48
问题 I have a PR, an object O to which the PR points, and an RQ set up for PR. I have a thread which keeps on polling the RQ, and at the first reference it finds in RQ, the thread prints the time at which it found it, and exits. Things work fine, but the moment O has a finalize (no matter how trivial), the thread no longer finds a reference in RQ and keeps running indefinitely. Question: why does it happen this way? I'm using Sun JDK 1.6. Here's the code: good case public class MyGCPhantom {

Meaning of ReferenceQueue

拜拜、爱过 提交于 2019-11-29 17:38:11
I try to understand class ReferenceQueue It is optional constructor argument for SoftReference and WeakReference Also it is mandatory argument for PhantomReference . According information I have read I can write some thesises a)for PhantomReference method get always returns null b) for Phantom references: 1. gc detect that object can be deleted from memory 2. reference to the object puted to the ReferenceQueue when we invoke clear or link to reference from queue becaom unreachable and gc see that 3. finalize methods invokes 4. free memory for weak/soft references: 1. gc detect that object can

When to use phantom references in Java? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-29 01:24:00
Possible Duplicate: Have you ever used Phantom reference in any project? I have read about the different types of reference. I understand how strong, soft and weak references work. But when I read about phantom references, I could not really understand them. Maybe because I could not find any good examples that show me what their purpose is or when to use them. Could you show me some code examples that use a phantom reference? I've never done this myself -- very few people ever need it -- but I think this is one way to do it. abstract class ConnectionReference extends PhantomReference

PhantomReference with null queue

为君一笑 提交于 2019-11-28 13:58:45
Java allow to write: new PhantomReference(new Object(), null) At this case new Object() will be collected? As I understand, phantom reference is alternative of finalize() method usage. And after appearing reference in queue, I need to do some additional actions and then run clear() java doc stays: It is possible to create a phantom reference with a null queue, but such a reference is completely useless: Its get method will always return null and, since it does not have a queue, it will never be enqueued What does mean if it will never be enqueued? As I understand it means that after finalize

Meaning of ReferenceQueue

戏子无情 提交于 2019-11-28 12:56:24
问题 I try to understand class ReferenceQueue It is optional constructor argument for SoftReference and WeakReference Also it is mandatory argument for PhantomReference . According information I have read I can write some thesises a)for PhantomReference method get always returns null b) for Phantom references: 1. gc detect that object can be deleted from memory 2. reference to the object puted to the ReferenceQueue when we invoke clear or link to reference from queue becaom unreachable and gc see

When to use phantom references in Java? [duplicate]

别等时光非礼了梦想. 提交于 2019-11-27 15:53:44
问题 I have read about the different types of reference. I understand how strong, soft and weak references work. But when I read about phantom references, I could not really understand them. Maybe because I could not find any good examples that show me what their purpose is or when to use them. Could you show me some code examples that use a phantom reference? 回答1: I've never done this myself -- very few people ever need it -- but I think this is one way to do it. abstract class

PhantomReference with null queue

冷暖自知 提交于 2019-11-27 08:19:52
问题 Java allow to write: new PhantomReference(new Object(), null) At this case new Object() will be collected? As I understand, phantom reference is alternative of finalize() method usage. And after appearing reference in queue, I need to do some additional actions and then run clear() java doc stays: It is possible to create a phantom reference with a null queue, but such a reference is completely useless: Its get method will always return null and, since it does not have a queue, it will never

Understanding Java's Reference classes: SoftReference, WeakReference, and PhantomReference

情到浓时终转凉″ 提交于 2019-11-27 02:30:00
Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference , but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or PhantomHashMap ? And if I use the following code... WeakReference<String> ref = new WeakReference<String>("Hello!"); if (ref != null) { // ref can get collected at any time... System.gc(); // Let's assume ref gets collected here. System.out.println(ref.get()); // Now what?! } ...what happens? Do I have to check if ref is null before every statement (this

Java: difference between strong/soft/weak/phantom reference

好久不见. 提交于 2019-11-26 19:17:32
I have read this article about the topic, but I don't really understand it. Please give me some advice along with examples when describing the concepts. Paolo Maresca Java provides two different types/classes of Reference Objects : strong and weak . Weak Reference Objects can be further divided into soft and phantom . Let's go point by point. Strong Reference Object StringBuilder builder = new StringBuilder(); This is the default type/class of Reference Object, if not differently specified: builder is a strong Reference Object. This kind of reference makes the referenced object not eligible

Understanding Java&#39;s Reference classes: SoftReference, WeakReference, and PhantomReference

纵然是瞬间 提交于 2019-11-26 10:07:48
问题 Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference , but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or PhantomHashMap ? And if I use the following code... WeakReference<String> ref = new WeakReference<String>(\"Hello!\"); if (ref != null) { // ref can get collected at any time... System.gc(); // Let\'s assume ref gets collected here. System.out.println(ref