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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:07:51+00:00 2026-06-06T07:07:51+00:00

I recently found out about the WeakHashMap data structure in Java. However, I don’t

  • 0

I recently found out about the WeakHashMap data structure in Java.

However, I don’t understand what it means by it garbage-collects a mapping when it is no longer in ordinary use. How does the data structure know I will no longer use a key in my program? What if I don’t refer to a key for a long time?

  • 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-06T07:07:53+00:00Added an answer on June 6, 2026 at 7:07 am

    However, I don’t understand what it means by it garbage-collects a mapping when it is no longer in ordinary use.

    OK. Under normal circumstances, when the garbage collector runs it will remove objects that your program can no longer use. The technical term is an "unreachable object", and it means that the program execution has no way to get hold of a reference to the object any more. An object that is unreachable, may be collected in the next GC cycle … or not. Either way, it is no longer the application’s concern.

    In this case, the WeakHashMap uses a special class called WeakReference to refer to the keys1. A weak reference is an object that acts sort of like an indirect pointer (a pointer to an object holding a pointer). It has the interesting property that the garbage collector is allowed to break the reference; i.e. replace the reference it contains with null. And the rule is that a weak reference to an object will be broken when the GC notices that the object is no longer reachable via a chain of normal (strong) or soft references2.

    The phrase "no longer in ordinary use" really means that the key object is no longer strongly or softly reachable; i.e. via a chain of strong and / or soft references.

    How does the data structure know I will no longer use a key in my program?

    The WeakHashmap doesn’t do it. Rather, it is the GC that notices that the key is not strongly reachable.

    As part of its normal traversal, the GC will find and mark all strongly reachable objects. Then it goes through all of the WeakReference objects and checks to see if
    the objects they refer to have been marked, and breaks them if they have not. (Or something like that … I’ve never looked at the actual GC implementation. And it is complicated by the fact that it has to deal with SoftReference and PhantomReference objects as well.)

    The only involvement that WeakHashmap has is that:

    • it created and uses WeakReference objects for the keys, and
    • it expunges hash table entries whose key WeakReferences have been cleared by the GC.

    What if I don’t refer to a key for a long time?

    The criterion for deciding that a weak reference should be broken is not time based.

    But it is possible that timing influences whether not a key is removed. For instance, a key could 1) cease to be strongly reference, 2) be retrieved from the map, and 3) be assigned to a reachable variable making it strongly referenced once again. If the GC doesn’t run during the window in which the key is not strongly reachable, the key and its associated value will stay in the map. (Which is what you’d want to happen …)


    1 – Implementation detail: in recent Java releases, the weak references actually refer to the map’s internal Entry objects rather than the keys. This allows broken references to be purged from the map more efficiently. Look at the code for details.
    2 – Soft references are a kind of reference that the GC is allowed to break if there is a shortage of heap memory.

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

Sidebar

Related Questions

I recently found out about java.util.Properties , which allows me to write and read
I recently found out about Cytoscape.js and would like to try out the force-directed
I recently found out about C# extension methods and wrote this one: /// <summary>
I recently found out about n-grams and the cool possibility to compare frequency of
Recently I have been learning about WMI and WQL. I found out the list
I recently found out about the awesomeness of the iTunes COM for Windows SDK.
I recently found out about Presenter First and read their whitepapers and blogs, etc.
I recently found out about oEmbed which is a fomat for allowing an embedded
Recently I found out about with and let statements for javascript. I'm interested in
I recently found out about Visual Studio's ability to create a Setup.exe file for

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.