Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phantom Referenced Objects

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

like image 550
Shimi Bandiel Avatar asked Sep 17 '08 07:09

Shimi Bandiel


People also ask

What are strong soft weak and phantom references in Java?

An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable. So in brief: Soft references try to keep the reference. Weak references don't try to keep the reference. Phantom references don't free the reference until cleared.

What's the difference between Softreference and Weakreference in Java?

A Soft reference is eligible for collection by garbage collector, but probably won't be collected until its memory is needed. i.e. garbage collects before OutOfMemoryError . A Weak reference is a reference that does not protect a referenced object from collection by GC.

What is a weak reference and how could it be useful to us?

As stated by Java documentation, weak references are most often used to implement canonicalizing mappings. A mapping is called canonicalized if it holds only one instance of a particular value. Rather than creating a new object, it looks up the existing one in the mapping and uses it.

Why do we use weak references?

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.


1 Answers

Edit, since I've misunderstand the question first:

Quoted from here http://www.memorymanagement.org/glossary/p.html:

The Java specification says that the phantom reference is not cleared when the reference object is enqueued, but actually, there's no way in the language to tell whether that has been done or not. In some implementations, JNI weak global references are weaker than phantom references, and provide a way to access phantom reachable objects.

But I found no other references which would say the same.

like image 53
jrudolph Avatar answered Nov 03 '22 04:11

jrudolph