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 9161869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:04:27+00:00 2026-06-17T14:04:27+00:00

Do SoftReference and WeakReference really only help when created as instance variables? Is there

  • 0

Do SoftReference and WeakReference really only help when created as instance variables? Is there any benefit to using them in method scope?

The other big part is ReferenceQueue. Besides being able to track which references are determined garbage, can Reference.enqueue() be used to forcibly register an object for garbage collection?

For example, would it be worth to create a method that takes some heavy memory resources (held by strong references) in an object and creating References to enqueue them?

Object bigObject;
public void dispose() {
    ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
    WeakReference<Object> ref = new WeakReference<Object>(bigObject, queue);
    bigObject = null;
    ref.enqueue();
}

(Imagine that Object in this case represents an object type that uses a lot of memory… like BufferedImage or something)

Does this have any realistic effect? Or is this just a waste of code?

  • 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-17T14:04:29+00:00Added an answer on June 17, 2026 at 2:04 pm

    One common idiom with reference queues is to e.g. subclass WeakReference to attach information that’s needed to clean up things, and then to poll a ReferenceQueue to get cleanup tasks.

    ReferenceQueue<Foo> fooQueue = new ReferenceQueue<Foo>();
    
    class ReferenceWithCleanup extends WeakReference<Foo> {
      Bar bar;
      ReferenceWithCleanup(Foo foo, Bar bar) {
        super(foo, fooQueue);
        this.bar = bar;
      }
      public void cleanUp() {
        bar.cleanUp();
      }
    }
    
    public Thread cleanupThread = new Thread() {
      public void run() {
        while(true) {
          ReferenceWithCleanup ref = (ReferenceWithCleanup)fooQueue.remove();
          ref.cleanUp();
        }
      }
    }
    
    public void doStuff() {
      cleanupThread.start();
      Foo foo = new Foo();
      Bar bar = new Bar();
      ReferenceWithCleanup ref = new ReferenceWithCleanup(foo, bar);
      ... // From now on, once you release all non-weak references to foo,
          // then at some indeterminate point in the future, bar.cleanUp() will
          // be run. You can force it by calling ref.enqueue().
    }
    

    For example, the internals of Guava’s CacheBuilder implementation when weakKeys are selected uses this approach.

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

Sidebar

Related Questions

SoftReference , WeakReference , PhantomReference may be used to customize the process of garbage
If I store bitmaps in a hashmap using SoftReference, will SoftReference call .recycle() on
I created a cache using Soft References a while ago, but in trying to
I want to write a cache using SoftReference s using as much memory as
I'm developing an application that need to load Bitmap. And using a SoftReference for
I'm using Lazy Loading List in my application and I need to load only
What's the difference between java.lang.ref.WeakReference and java.lang.ref.SoftReference ?
I know there is a WeakHashMap in java.util , but since it uses WeakReference
Until now I'm using a SoftReference Cache for images in Android. This cache is
// Global cache of images. // Using SoftReference to allow garbage collector to clean

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.