12

I'm not used to deal with Soft and Weak references in Java but i understand the principle since i'm used to deal with datagrids like Gemfire, which provide overflow to hdd features when memory is full, probably using soft references or something similar i guess.

What i don't understand in Guava is that it provides methods to make the keys soft/weak, and the values soft/weak.

I just wonder what's the point of creating soft keys with non-soft values for exemple? I mean, when the soft references start to be collected, we can't find anymore the entry by its key, so why would we like the values to stay in the map?

Can someone give us some usecases with:

  • Weak key / soft values
  • Weak key / normal values
  • Soft key / weak values
  • Soft key / normal values

Thanks


Edit I'm not sure my question is precise enough so what i'd like to know is:

  • when a key is collected (weak/soft), what happens to the value (non weak/soft)
  • when a value is collected (weak/soft), what happens to the key
  • Are entries with a missing part (key or value) kept in the cache?
  • And is there any usecase when you want such entries to stay in the cache.

Edit: As discussed on Kevin Bourillon's answer, finally i think i understand why using soft keys doesn't mean anything. Here's why:

static class KeyHolder {
    final private String key;
    public KeyHolder(String key) {
        this.key = key;
    }
    public String getKey() {
        return key;
    }
    @Override
    public boolean equals(Object o) {
        KeyHolder that = (KeyHolder)o;
        boolean equality = this.getKey().equals(that.getKey());
        return equality;
    }
    @Override
    public int hashCode() {
        return key != null ? key.hashCode() : 0;
    }
    @Override
    public String toString() {
        return "KeyHolder{" +
                "key='" + key + '\'' +
                '}';
    }
}

public static void main(String[] args) {
    System.out.println("TESTING WEAK KEYS");
    testMap( new MapMaker().weakKeys().<KeyHolder,String>makeMap() );


    System.out.println("\n\n");
    System.out.println("TESTING SOFT KEYS");
    testMap(new MapMaker().softKeys().<KeyHolder, String>makeMap());


    System.out.println("\n\n");
    System.out.println("TESTING SOFT REFERENCES");
    KeyHolder key1 = new KeyHolder("toto");
    KeyHolder key2 = new KeyHolder("toto");
    SoftReference<KeyHolder> softRef1 = new SoftReference<KeyHolder>(key1);
    SoftReference<KeyHolder> softRef2 = new SoftReference<KeyHolder>(key2);
    System.out.println( "equals keys? " + key1.equals(key2) );
    System.out.println( "equals ref? " + softRef1.equals(softRef2) );
}

private static void testMap(Map<KeyHolder,String> map) {
    KeyHolder strongRefKey = new KeyHolder("toto");
    KeyHolder noStrongRefKey = new KeyHolder("tata");
    map.put(strongRefKey,"strongRef");
    map.put(noStrongRefKey,"noStrongRefKey");
    // we replace the strong reference by another key instance which is equals
    // this could happen for exemple in case of serialization/deserialization of the key
    noStrongRefKey = new KeyHolder("tata");
    System.gc();
    System.out.println( "strongRefKey = " + map.get(strongRefKey) );
    System.out.println( "noStrongRefKey = " + map.get(noStrongRefKey) );
    System.out.println( "keyset = " + map.keySet() );
}

This code produces the output:

TESTING WEAK KEYS
strongRefKey = strongRef
noStrongRefKey = null
keyset = [KeyHolder{key='toto'}]



TESTING SOFT KEYS
strongRefKey = strongRef
noStrongRefKey = null
keyset = [KeyHolder{key='tata'}, KeyHolder{key='toto'}]



TESTING SOFT REFERENCES
toto == toto -> true
equals keys? true
equals ref? false

As you can see, with the (deprecated) soft keys map, the KeyHolder containing "tata" still exists in the map. But notice that i'm still not able to find my entry with a newly created key "new KeyHolder("tata");" This is because, my keys are meaningfully equals, but the reference wrappers around them are not equals because their equals method is not overriden in Guava! In this case, yes, softKeys doesn't mean anything since you absolutly need to keep an identity reference to that key to be able to retrieve it.

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419

2 Answers2

15

softKeys never makes sense, so we removed the method. softValues is the only way soft refs make sense, assuming the value instances aren't also reachable in other ways outside the cache.

Then usage of weakKeys basically boils down to whether you want identity equality for keys. If the key overrides equals and you need that equality behavior, you can't use it. If you want identity, then weakKeys is how you get that, and it also makes sense because once all other references to the key have been GC'd, there would be no way to look up that entry anyway, so it might as well be removed.

I actually am not entirely clear on when weakValues is useful, and was going to look into it. It would probably be a case where weakKeys is not an option (say, Integer keys), and where the values are ordinarily strongly referenced through other means, like some sort of session object, but when that object goes away, it signifies that no one will be looking for this in the cache anymore. It seems slightly farfetched when I put it that way, though.

Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
  • So if you use softvalues and most of the values get collected, you still have a cache with a lot of entries in it, a lot of keys, but no value at all? – Sebastien Lorber Jun 11 '12 at 18:07
  • 2
    So if you use weak keys and you don't have anymore the keys reference outside the cache, the keys get collected but what happens to the values? Are they kept in the cache? – Sebastien Lorber Jun 11 '12 at 18:11
  • 1
    When you say softKeys doesn't make sense, i'm not sure of that. What about if you have the means of reconstructing a new key: you may want the keys to be kept in the cache, even if you don't have any identity reference, since you CAN create again the appropriate key no? What about the key being cloned/serialized/deserialized. Do you see what i mean – Sebastien Lorber Jun 11 '12 at 18:17
  • @Sebastien Lorber When the values and/or the keys get collected, their entries get evicted from the cache as well (although not immediately), see "When Does Cleanup Happen?" [here](http://code.google.com/p/guava-libraries/wiki/CachesExplained#Eviction). Concerning softKeys making sense, I agree with you, but I'm afraid that the Guava guys know it better. – maaartinus Jun 12 '12 at 09:49
  • 1
    In almost any case where `softKeys` would seem to work, `weakKeys` works better. Once the key is no longer strongly reachable, `weakKeys` allows it be GC'd. `softKeys` might hold it around arbitarily longer -- but to what purpose? No one can ever look up that entry, because as already noted, the key isn't strongly reachable anymore! So we're down to edge cases like "oh, but all my _other_ references to the key are definitely soft as well," or "oh but I sometimes iterate over the asMap() entries and revive certain keys for some reason"... these situations never actually came up. – Kevin Bourrillion Jun 13 '12 at 00:35
  • 4
    @KevinBourrillion, please check my edit. As i said, a key could be serialized,deserialized for exemple. And i think soft keys would make sense if Guava was overriding equals method of the SoftReference. I've seen that Guava uses an "equivalence mecanism" and i think, for soft references, the defaultEquivalence should not be identity but equals, to handle such a case. Then, using softKeys would make sense since you would be able to retrieve the map value by a copy of the key. What do you think? – Sebastien Lorber Jun 15 '12 at 09:08
  • answer accepted but it would be nice if you tell me what you think of my edit :) – Sebastien Lorber Jul 03 '12 at 15:18
  • @KevinBourrillion It would be nice if your explanation concerning the removal of softKeys() was mentioned in the API documentation or at least in the source code, for example in setKeyStrength(Strength). – gouessej Sep 04 '14 at 12:20
  • @KevinBourrillion I am currently trying out a weak value cache to reference results of running multi-threaded jobs. I don't think I would do it like this if I was writing the system new, but it seems a good solution in that it guarantees that I wont lose reference to the result by id while it is running and that it will be evicted after completion. I'm not sure whether it's a good idea yet though – MikeB Dec 03 '14 at 09:47
2

The non-Guava WeakHashMap is based on weak keys and discards the whole entry when a key is no longer in use. Thus, there is no "value staying in the map" then. This is the kind of behaviour I would intuitively expect from such a data structure and I strongly suspect that Guava behaves the same way, also for collected soft references.

Note that the Java Collections library only provides a weak key / normal values map, not weak keys /soft values or weak keys / weak values, as the user can do the value wrapping themself (but cannot do likewise with the keys as the lookup in a hashmap should not be expected to work the same way for the actual keys and weak references to the keys).

I myself used a WeakHashMap once to associate objects that could become obsolete by a mechanism somewhere else with information unrelated to that mechanism (to be exact: moving balls that could be destroyed by certain collisions with information on their recent movement). These values were created specifically for that map and not referenced elsewhere (except locally in methods that could only be called while their keys still existed). Thus, there was no need to use anything other than normal references to the values as when the key became obsolete, the value would become unreferenced and garbage-collectible.

arne.b
  • 4,212
  • 2
  • 25
  • 44
  • 1
    Note that there is a caveat when using weak keys: "Warning: when this method [weakKeys()] is used, the resulting cache will use identity (==) comparison to determine equality of keys." – thSoft Jan 26 '13 at 12:45