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

  • SEARCH
  • Home
  • 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 8043693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:59:14+00:00 2026-06-05T04:59:14+00:00

I want to properly close Closeable object when it’s no longer referenced by other

  • 0

I want to properly close Closeable object when it’s no longer referenced by other threads.

I wrote some small test, but after object is enqueued the get method return null, i.e. the poll method returns proper Object which has no referent.

  public static void main(String[] args)
  {
   ReferenceQueue<Closeable> reaped = new ReferenceQueue<Closeable>();
   Closeable s = <SOME CLOSEABLE IMPL>;
   WeakReference<Closeable> ws = new WeakReference<Closeable>(s, reaped);
   s = null;

   System.gc();
   Closeable ro = (Closeable)reaped.poll().get();
   ro.close();
  }

Thanks in advance.
Any help will be appreciated.

  • 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-06-05T04:59:15+00:00Added an answer on June 5, 2026 at 4:59 am

    First, if it is only about closing, use PhantomReference.
    Next, from the reference queue, poll() does not guarantee that you will get the reference back. and you will never get the actual object (referent) back.

    If you want to make sure your Closeables are closed you have to keep track of them yourself lets say in a Map<Reference<?>, Closeable>. Then when you poll() your reference queue, you will eventually get the ref then you have to use it to get the Closeable from the map.

       class MyThing {
          Closeable c;
       }
    
       Map<Reference<MyThing>, Closeable> m = new HashMap();
       ReferenceQueue<MyThing> reaped = new ReferenceQueue<MyThing>();
    
       MyThing mt = new MyThing();
       mt.c = new MyClosable();
    
       Reference<MyThing> pref = new PhantomReference<MyThing>(mt, reaped);
       m.put(pref, mt.c);
    
       mt = null;
    
    
       System.gc();
       Reference<MyThing> rf = reaped.poll();
       while (rf != null) {
         m.get(rf).close(); 
         rf = reaped.poll();
       }
    

    Note If you don’t have a real reason to do this or if you do not understand what are you really doing, DO NOT do this kind of thing.

    You can close your files in finally and BTW if it is about files, sockets, etc, they are closed for you (they already implement finalize()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used seekToTime in my application and its working properly. But I want
I have a class in C# where I want to close out some communication
I set auto complete property. Now, when anyone close browser, i want to clear
I want to make sure, the app will work properly before I warble it.
I've little experience with advanced OO practices, and I want to design this properly
I want apply margin property for my html newsletter But Gmail ignores this CSS
I want send some data to a remote webpage from my site. Actually it
I have some code that that I only want to execute if curl_init() is
I have a python test in which I want to test if the logging
I have 5 processes p1,p2,...,p5 where I want to write some data to stdin

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.