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
2
votes
0 answers

Java: practical use of PhantomReference?

Possible Duplicate: Have you ever used Phantom reference in any project? What are practical uses of PhantomReference? Since their 'get' always returns null, the objects they point to can't be accessed through them. So what's their use? Through…
shrini1000
  • 7,038
  • 12
  • 59
  • 99
1
vote
1 answer

Why aren't bytebuffer collected when using FinalizableReferenceQueue?

I have a buffer pool implementation which basically provides pre-allocated ByteBuffer objects via allocate()/release() API. In order to detect the cases when caller forgot to call release and the ByteBuffer ref is leaked, I am using Guava's…
Tushar Goyal
  • 212
  • 3
  • 15
1
vote
2 answers

Reference Queue is Always Empty

I am trying to clean a native resource when it is not accessible anymore. That resource provides a method to clean allocated resources (memory, threads etc.). To achieve this, I used Phantom Reference. That resource should be created asynchronously…
Emre Turan
  • 101
  • 1
  • 9
1
vote
2 answers

Mixing different reference types in one collection

SoftReference, WeakReference, PhantomReference may be used to customize the process of garbage collection. All of them extend Reference therefore it is possible to mix them in single collection. Hard references (most common ones) do no extend…
1
vote
1 answer

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

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…
Marcono1234
  • 5,856
  • 1
  • 25
  • 43
1
vote
2 answers

why is phantom reference not queued?

I am leaning phantom reference ,and I got confused how is a phantom reference enqueued when the referent is being garbage-collected. Here is my code Object s = new Object(); ReferenceQueue queue = new ReferenceQueue(); …
Tony Hu
  • 11
  • 2
1
vote
1 answer

Inner fields of a class are not collected by GC via Phantom References

I am having problems with Phantom References when referents are the fields inside the class. When class objects are set to null, fields are not collected automatically by GC Controller.java public class Controller { public static void main(…
GoT
  • 530
  • 1
  • 13
  • 35
1
vote
1 answer

What is the relationship between the PhantomReference and ReferenceQueue?

I am having a little confusion about the PhantomReference in java. When I looked in online articles most of them mentioned that the PhantomReference object will return null after creation even if we invoke get() on the reference and also it…
Dilan
  • 1,389
  • 5
  • 14
  • 24
0
votes
0 answers

How to solve object object problem in Phantomjs?

I tried all the Id and class options. I tried to pull the element using the last tag. But it writes [object Object] expression to the console. This object cannot be fragmented in any way. var webpage = require('webpage'); var page =…
0
votes
1 answer

Optimal way to use phantom references in Java 8, or use weak references instead?

I am implementing a feature that reports an error when instances of my Java class are discarded before being "used" (for simplicity, we can define being "used" as having a particular method called). My first idea is to use phantom references, which…
user102008
  • 30,736
  • 10
  • 83
  • 104
0
votes
0 answers

How to run a cleaning action with a reference to the object being registered

Since java 9 we finally have Cleaner. However in the docs is specifically declared: Each cleaner operates independently, managing the pending cleaning actions and handling threading and termination when the cleaner is no longer in use. Registering…
elect
  • 6,765
  • 10
  • 53
  • 119
0
votes
1 answer

any way to tell if a method ended or local in method in no longer in use?

I need some solution that will help me tell for a some methods, in a case like this: void myMethod(...) { MyObject obj = new MyObject(); // do stuff } if this method has ended or if obj is unreachable (which will indicate the method has…
user967710
  • 1,815
  • 3
  • 32
  • 58
0
votes
0 answers

How to unload a ClassLoader that contains static native resources in Java 9

I have a library that provide a Resource class, which should be cleaned by Java 9 Cleaner. import java.lang.ref.Cleaner; public class Resource { static Cleaner CLEANER = Cleaner.create(); static class CleanerHandler implements Runnable { …
Yang Bo
  • 3,586
  • 3
  • 22
  • 35
0
votes
2 answers

Use of clear in java.lang.ref.Reference class

ReferenceQueue q = new ReferenceQueue(); Reference r = q.remove(); r.clear(); I see that the java doc says that the clear method clears this reference object. I don't understand the meaning of this. Does this clear from the memory and thus in other…
user3276247
  • 1,046
  • 2
  • 9
  • 24
0
votes
1 answer

Phantom reference not freed up during GC

I am slightly confused with the usage of phantom references. I read that when Object which only has Phantom reference pointing them can be collected whenever Garbage Collector likes it. But, it is not working as intended in my example. import…
GoT
  • 530
  • 1
  • 13
  • 35