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
3
votes
4 answers

is there a way to recycle a complex java object once the GC has decided it is unreachable

In C++ I use reference counted objects to impplement a for of "auto" recycling object pool SmartPointer object = pool.getObject(); // hold reference // ... do stuff with object over time. object = nullptr; // that is when reference …
3
votes
1 answer

Why exactly PhantomReference should be preferred to finalize?

They both can be used for cleanup, there is almost no guarantees, but PR requires more harness coding. So, having two options, why exactly I have to prefer one to another? Javadoc 9 describes finalize as very problematic, but that doesn't make its…
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
3
votes
2 answers

Why enqueuing of PhantomReference takes more GC cycles than WeakReference or SoftReference?

I decided to continue https://stackoverflow.com/a/41998907/2674303 in a separated topic. Let's consider following example: public class SimpleGCExample { public static void main(String[] args) throws InterruptedException { …
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
3
votes
1 answer

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

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

Is it safe to have a PhantomReference to `this`?

I have a shared resource for which I would like to know how many other objects are still using this resource. To do this I would like to use PhantomReferences. Since ReferenceQueues do not keep track of the references registered for them (source,…
Marcono1234
  • 5,856
  • 1
  • 25
  • 43
3
votes
2 answers

Phantom Contact Account Appearing - Android

I am writing an app that allows the user to select from a list of names and then add one of the names (with phone number, etc) to their contacts. When I retrieve my phone's list of Contact Accounts, I am getting 3 results: "Phone",…
Chris
  • 5,485
  • 15
  • 68
  • 130
3
votes
2 answers

How Phantom reference works?

The API doc says This reference type differs from the others in that it isn't meant to be used to access the object, but as a signal that the object has already been finalized, and the garbage collector is ready to reclaim its memory. If Phantom…
Ravi Gupta
  • 4,468
  • 12
  • 54
  • 85
3
votes
4 answers

What is a Ghost Reference?

I was just wondering what a ghost reference was? Is it that you don't have to refer to a object? EDIT: Sorry, I wasn't clear, the langauge is Java, and I read it about linked lists. While reading a problem to write a LinkedQueue iterator to support…
Roxy
  • 79
  • 1
  • 5
  • 9
2
votes
2 answers

When a PhantomReference/SoftReference/WeakReference is queued, how do you know what it referred to?

I haven't used PhantomReferences. There seems to be very few good examples of real-world use. When a phantom shows up in your queue, how do you know which object it is/was? The get() method appears to be useless. According to the JavaDoc,…
Ed Staub
  • 15,480
  • 3
  • 61
  • 91
2
votes
2 answers

why PhantomReference does not work?

Demo code: import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; public class Main { public static void main(String[] args) throws InterruptedException { Object test = new Object(); ReferenceQueue q…
boiledwater
  • 10,372
  • 4
  • 37
  • 38
2
votes
1 answer

Alternative to Java finalizer

I am implementing a locking service in a distributed system using Mysql GET_LOCK. Upon calling my getLock() method, if a lock is obtained by a client, I make an entry to DB and delete the entry when lock is released. Assumption is calling client…
dhamu
  • 605
  • 1
  • 7
  • 17
2
votes
1 answer

Why we are obliged to clear a PhantomReference manually?

From javadoc: Unlike soft and weak references, phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared…
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
2
votes
3 answers

How finalize method works with phantom reference in java

Hi I have one doubt about phantom reference. What I understand the finalize method is called just before when object are going for garbage collection. But some time if object are not eligible for garbage collection then finalize method will not…
user711466
  • 109
  • 1
  • 3
2
votes
2 answers

Meaning of ReferenceQueue

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…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
2
votes
1 answer

Example of using PhantomReference

As far as I know reference fall into QueueReference when an object that was pointed by the reference gets deleted. Here an example where I was about to demonstrate this, but it doesn't work. The code inside if has never been executed. What does it…
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192