phantom-reference

Phantom Referenced Objects

℡╲_俬逩灬. 提交于 2019-12-12 08:01:26
问题 Phantom References serve for post-mortem operations. The Java specification states that a phantom referenced object will not be deallocated until the phantom-reference itself is cleaned. My question is: What purpose does this feature (object not deallocated) serve? (The only idea i came up with, is to allow native code to do post-mortem cleanup on the object, but it isn't much convincing). 回答1: The only good use-case I can think of, that would prevent deallocation, is one where some kind of

Rationale for Soft-/Weak-/PhantomReferences clearing references to objects which have reference to tracked object

徘徊边缘 提交于 2019-12-11 06:15:58
问题 The documentation for Soft-, Weak- and PhantomReferences all include a line simiar to the following (taken from PhantomReference ): At that time it will atomically clear all phantom references to that object and all phantom references to any other phantom-reachable objects from which that object is reachable. The part which is confusing me is the one about the other phantom-reachable objects. If I understand it correctly this describes this case: Objects: A B References: -> : Strong reference

Why won't my objects die?

只谈情不闲聊 提交于 2019-12-07 03:47:58
问题 I'm trying to implement a mechanism that deletes cached files when the objects that hold them die, and decided to use PhantomReference s to get notified on garbage collection of an object. The problem is I keep experiencing weird behavior of the ReferenceQueue . When I change something in my code it suddenly doesn't fetch objects anymore. So I tried to make this example for testing, and ran into the same problem: public class DeathNotificationObject { private static ReferenceQueue

How to use PhantomReference as finalize() Replacement

試著忘記壹切 提交于 2019-12-07 03:09:44
问题 Javadoc 8 for PhantomReference states: Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. So I tried creating a thread that is calling the close() method of a Test Object that is eligible for garbage collection. The run() tries to get all Test Objects pre-mortem . Actually the retrieved Test Objects are all null . The expected behavior is, that the Test Objects are retrieved and the

How to use PhantomReference as finalize() Replacement

倾然丶 夕夏残阳落幕 提交于 2019-12-05 08:04:09
Javadoc 8 for PhantomReference states: Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. So I tried creating a thread that is calling the close() method of a Test Object that is eligible for garbage collection. The run() tries to get all Test Objects pre-mortem . Actually the retrieved Test Objects are all null . The expected behavior is, that the Test Objects are retrieved and the close method is called. No matter how many Test Objects you create there is not a single Test Object that

Why since java 9 PhantomReference java doc states that it is dedicated to the POST-mortem cleanup actions although it was PRE-mortem before

拟墨画扇 提交于 2019-12-05 07:41:22
问题 PhantomReference java doc for java 8 and less looks like this: Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. If the garbage collector determines at a certain point in time that the referent of a phantom reference is phantom reachable, then at that time or at

Why won't my objects die?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 06:47:01
I'm trying to implement a mechanism that deletes cached files when the objects that hold them die, and decided to use PhantomReference s to get notified on garbage collection of an object. The problem is I keep experiencing weird behavior of the ReferenceQueue . When I change something in my code it suddenly doesn't fetch objects anymore. So I tried to make this example for testing, and ran into the same problem: public class DeathNotificationObject { private static ReferenceQueue<DeathNotificationObject> refQueue = new ReferenceQueue<DeathNotificationObject>(); static { Thread deathThread =

When to use Weak and Phantom references in Java

让人想犯罪 __ 提交于 2019-12-04 21:14:12
问题 I read many articles, but I don't understand - where do I need to use Weak and Phantom references in practice? Soft references - is a good choice for cache, as I understand. But weak and phantom, I don't know when to use. Please provide examples of real tasks where we need to use them. 回答1: You can use weak references for cache, simply like soft references as you said. What good are PhantomReferences? I'm only aware of two serious cases for them: first, they allow you to determine exactly

Why since java 9 PhantomReference java doc states that it is dedicated to the POST-mortem cleanup actions although it was PRE-mortem before

情到浓时终转凉″ 提交于 2019-12-03 21:58:59
PhantomReference java doc for java 8 and less looks like this: Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. If the garbage collector determines at a certain point in time that the referent of a phantom reference is phantom reachable, then at that time or at some later time it will enqueue the reference. In order to ensure that a reclaimable object remains so

Java: PhantomReference, ReferenceQueue and finalize

南笙酒味 提交于 2019-12-01 05:21:16
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 { public static void main(String[] args) throws InterruptedException { GCPhantomObject p = new GCPhantomObject