Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6594367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:48:06+00:00 2026-05-25T17:48:06+00:00

Demo code: import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; public class Main { public static void main(String[]

  • 0

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<Object> q = new ReferenceQueue<Object>();
        PhantomReference<Object> p = new PhantomReference<Object>(test, q);
        Object lock = new Object();
        while (true) {
            synchronized (lock) {
                                 //q.poll() is null always,why?
                if (q.poll() != null) {
                    break;
                }
                //System.gc();
                lock.wait();
            }
        }
        System.out.println(1111111);
    }
}

I tested the code,but it always is dead loop.
The code (System.out.println(1111111);) can not execute,q.poll() reurn null.

I think if test object is removed by GC,q.poll() will return p object,then break loop,but invoke this demo code,it is not like my thought

Edited:
I modify the demo code,it can work now.

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<Object> q   = new ReferenceQueue<Object>();
        PhantomReference<Object> p = new PhantomReference<Object>(test, q);
        Object lock = new Object();
        while (true) {
            synchronized (lock) {
                if (q.poll() != null) {
                    break;
                }
                test = null; //it is important  
                System.gc();
                lock.wait(100);//should not lock.wait()
            }
        }
        System.out.println(1111111);
    }
}

AS sb says,the statement( test=null) is key.GC collect test object that assign null.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:48:07+00:00Added an answer on May 25, 2026 at 5:48 pm

    The fact that the test variable is still present means the GC will never collect the object it refers to… I believe JVMs don’t collect anything that a local variable refers to even if the local variable is never referred to in the rest of the method.

    (Also, it’s not clear why you’re using lock.wait() when nothing is calling lock.pulse().)

    Here’s code which works for me:

    import java.lang.Thread;
    import java.lang.ref.PhantomReference;
    import java.lang.ref.ReferenceQueue;
    
    public class Test {
        public static void main(String[] args) throws InterruptedException {
            Object test = new Object();
            ReferenceQueue<Object> q = new ReferenceQueue<Object>();
            PhantomReference<Object> p = new PhantomReference<Object>(test, q);
            Object lock = new Object();
            while (true) {
                System.out.println("Checking queue...");
                if (q.poll() != null) {
                    break;
                }
                System.out.println("Still polling...");
                System.gc();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    System.out.println("Interrupted!");
                    break;
                }
                // Make the object garbage...
                test = null;
            }
            System.out.println("Finished.");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.io.*; public class Demo{ public static void main(String[] args){ File f = new
Here is come code to demo my problem: Parent class: import java.awt.Color; import java.awt.Dimension;
Let's look at the following code snippet in Java. package demo; import java.util.Calendar; final
This is the complete code : import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.Thread;
I have been playing with the demo code from this msdn article by Jeffrey
I downloaded the qt embedded demo source code recently on my linux machine. Following
I'll do a demo of my code to slightly non-technical audience, and I need
Bellow is the code behind and the Xaml for a demo app to review
I ran the code to create the generically related objects from this demo: http://www.djangoproject.com/documentation/models/generic_relations/
I've got code like that: <ul class=gallery_demo_unstyled> <li class=active><img src='001.jpg' /></li> <li><img src='002.jpg' /></li>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.