Questions tagged [phantom-reference]

A Phantom Reference refers to a reference type of objects in java. It is the weakest of the reference types. If an object is finalized, but its memory is not yet reclaimed, is phantomly referenced. Alternative of deprecated finalization via finalize() method

A Phantom Reference refers to a reference type of objects in java. It is the weakest of the reference types. If an object is finalized, but its memory is not yet reclaimed, is phantomly referenced.

See also JavaDoc and Wikipedia.

51 questions
212
votes
7 answers

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

I have read this article about different types of references in Java (strong, soft, weak, phantom), but I don't really understand it. What is the difference between these reference types, and when would each type be used?
user1204873
82
votes
4 answers

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

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…
18
votes
1 answer

When to use phantom references in Java?

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…
hqt
  • 29,632
  • 51
  • 171
  • 250
9
votes
2 answers

How to use PhantomReference as finalize() Replacement

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…
notes-jj
  • 1,437
  • 1
  • 20
  • 33
8
votes
6 answers

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…
Shimi Bandiel
  • 5,773
  • 3
  • 40
  • 49
8
votes
1 answer

Delete native peer with general PhantomReference class

As Hans Boehm in the Google I/O '17 talk "How to Manage Native C++ Memory in Android" suggests I use the PhantomReferenceclass to ensure native peers are deleted properly. In the linked video at 18 min 57 sec he shows an example implementation of an…
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
8
votes
4 answers

When to use Weak and Phantom references in Java

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…
7
votes
1 answer

Understanding Phantom reference vs weak reference with respect to Reference queue

As per the link https://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html, PhantomReferences are enqueued only when the object is physically removed from memory and WeakReferences are enqueued before finalization or garbage…
Anand
  • 20,708
  • 48
  • 131
  • 198
6
votes
1 answer

Why won't my objects die?

I'm trying to implement a mechanism that deletes cached files when the objects that hold them die, and decided to use PhantomReferences to get notified on garbage collection of an object. The problem is I keep experiencing weird behavior of the…
Elist
  • 5,313
  • 3
  • 35
  • 73
5
votes
1 answer

Java PhantomReference vs finalize()

I've been reading this article about PhantomReference https://www.baeldung.com/java-phantom-reference and simplified sample code found there: public static void main(String[] args) throws InterruptedException { ReferenceQueue
Maciej
  • 117
  • 3
  • 8
5
votes
1 answer

PhantomReference with null queue

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…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
5
votes
4 answers

Java: PhantomReference, ReferenceQueue and finalize

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…
shrini1000
  • 7,038
  • 12
  • 59
  • 99
4
votes
2 answers

What is referent in java?

What does referent mean in java language? I came across this word while reading about phantom references in java but it was not explained there, just used (the word). And I could not find the answer by searching on Google either. What I could…
nikola3103
  • 113
  • 1
  • 9
4
votes
1 answer

Uses of different reference types in Java

I've recently been playing around with soft, weak and phantom reference types in Java and have been wondering if there's any uses out there for them that I haven't come across. I've used them in the past for various things and they've always fallen…
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
4
votes
1 answer

Can a PhantomReference stop its referent from memory reclamation?

My question that sums it all up: Can a strongly reachable Java PhantomReference stop its referent object's memory from being reclaimed by the Garbage Collector (GC)? Details follow: Callum posted this question as well but it is not answered…
1
2 3 4