跳至主要内容

WeakHashMap implementation detail

WeakHashMap implementation detail

What is weak reference

So let’s start with weak reference.
Weak reference get this name compared with the normal reference, which is also called strong reference. Weak reference is weak enough, and can’t retain the object it refer to in the memory. But why we need a reference when we can’t guarantee we can reach this object?
Here is an example from this post:

suppose we find ourselves needing to keep track of each Widget’s serial number, but the Widget class doesn’t actually have a serial number property – and because Widget isn’t extensible, we can’t add one. No problem at all, that’s what HashMaps are for:
serialNumberMap.put(widget, widgetSerialNumber);
This might look okay on the surface, but the strong reference to widget will almost certainly cause problems. We have to know (with 100% certainty) when a particular Widget’s serial number is no longer needed, so we can remove its entry from the map. Otherwise we’re going to have a memory leak (if we don’t remove Widgets when we should) or we’re going to inexplicably find ourselves missing serial numbers (if we remove Widgets that we’re still using).

More

At previous part, we just mention ‘weak reference’. Actually, ‘weak reference’ has three different kinds(level), from stronger to weaker: soft, weak, phantom, and we will describe it now.

Soft vs Weak

SoftReferences aren’t required to behave any differently than WeakReferences, but in practice softly reachable objects are generally retained as long as memory is in plentiful supply. This makes them an excellent foundation for a cache, since you can let the garbage collector worry about both how reachable the objects are (a strongly reachable object will never be removed from the cache) and how badly it needs the memory they are consuming.

Weak vs Phantom

in theory the object could even be “resurrected” by an unorthodox finalize() method, but the WeakReference would remain dead. The difference is in exactly when the enqueuing happens. WeakReferences are enqueued as soon as the object to which they point becomes weakly reachable. This is before finalization or garbage collection has actually happened. PhantomReferences are enqueued only when the object is physically removed from memory, and the get() method always returns null specifically to prevent you from being able to “resurrect” an almost-dead object.

Usage

An example of weak reference in java library is WeakHashMap. Let’s dive into source to see how it is implemented and how it is related with weak reference.

WeakHashMap: who is weak

From document:

Hash table based implementation of the Map interface, with weak keys.

And the source code of WeakHashMap:

private static class Entry<K,V> extends
WeakReference<Object> implements Map.Entry<K,V> {
  V value;
  final int 
  Entry<K,V> next;

  Entry(Object key, V value,
      ReferenceQueue<Object> queue,
      int hash, Entry<K,V> next) {
    super(key, queue); // <-------------
    // **here we send key to WeakReference**
    this.value = value;
    this.hash  = hash;
    this.next  = next;
  }
  //...
}

So this clear some misunderstanding of WeakHashMap: key is weakly reached, not value. In other word, when key is a large class and may cause memory leak, you may need WeakHashMap. And If your value is your worry, you have to do it yourself:

HashMap<K,WeakReference<V>>

or use library like Guava:

ConcurrentMap<Long, CustomObject> graphs = new MapMaker()
   .weakValues()
   .makeMap();

What happen when key is reclaimed by GC

As the document say:

An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed.

What the doc mean is that entry will be set null to make it available for GC.

And the source of function expungeStaleEntries():

for (Object x; (x = queue.poll()) != null; ) {
    synchronized (queue) {
        @SuppressWarnings("unchecked")
        Entry<K,V> e = (Entry<K,V>) x;
        int i = indexFor(e.hash, table.length);

        Entry<K,V> prev = table[i];
        Entry<K,V> p = prev;
        while (p != null) {
            Entry<K,V> next = p.next;
            if (p == e) {
                if (prev == e)
                    table[i] = next;
                else
                    prev.next = next;
                // Must not null out e.next;
                // stale entries may be in use
                // by a HashIterator
                e.value = null; // Help GC
                size--;
                break;
            }
            prev = p;
            p = next;
        }
    }
}

This code will be called when resize()/size() related job is needed.

A code snippet of code tuning in it

In the hash-based map implementation, we can find a snippet of conversion from hash code to table index as following:

/**
 * Returns index for hash code h.
 */
private static int indexFor(int h, int length) {
    return h & (length-1);
}

Here, we can see that they use length-1 & hash to calculate the position of a new node. In our algorithm course, we can learn that we spread the element into a array-based hash-table by hash % n. So why we can replace it with & (which is more efficient than %)?

We take some number to test it:

n hash n-1 & hash hash % n
10 11 9 1
10 12 8 2
10 13 9 3

It seems that they are not same or even equivalent at all? Take a close look at the source code, we learn that n in xxxHashMap is always the power of 2 and n-1 & hash == hash % n will always be true.
Maybe this is the reason of choosing 2^n as length, not as some general algorithms suggest using prime number.

Usage of WeakHashMap

TheadLocal implementation also rely on this: every thread has a ThreadLocalMap which is actually a WeakHashMap<ThreadLocal<?>, Object> to store every thread’s variable. After your program doesn’t hold a reference to your ThreadLocal<?> variable, it will be recycled soon even your thread is still running. In that case, variable will not retain in memory to cause memory leaking and no need to worry about it for programmers.

Ref

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi...

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation ...

Learn Spring Expression Language

When reading the source code of some Spring based projects, we can see some code like following: @Value( "${env}" ) private int value ; and like following: @Autowired public void configure (MovieFinder movieFinder, @ Value ("#{ systemProperties[ 'user.region' ] } ") String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } In this way, we can inject values from different sources very conveniently, and this is the features of Spring EL. What is Spring EL? How to use this handy feature to assist our developments? Today, we are going to learn some basics of Spring EL. Features The full name of Spring EL is Spring Expression Language, which exists in form of Java string and evaluated by Spring. It supports many syntax, from simple property access to complex safe navigation – method invocation when object is not null. And the following is the feature list from Spring EL document : ...