weakhashmap

Java Weak Hash Map - Need to remove entry based on weakness of value, not key

只谈情不闲聊 提交于 2019-12-05 05:24:47
So the Java WeakHashMap lets one create a map whose entries are removed if its keys become weak. But how can I create a Map whose entries are removed when the values in the map become weak? The reason I want to use a map is as a Global Hash Table which keeps track of objects according to their ID's. ID ---> Object Address Key ---> Value (Where ID is a text string) I want key-value pairs to be removed when the object addresses become weak, not the Strings that point to them. Anyone any thoughts on this? Such a map is supported, for example, in Guava : Map<..., ...> m = new MapMaker().weakValues

WeakHashMap and strongly referenced value

怎甘沉沦 提交于 2019-12-03 06:46:52
Javadocs says "When a key has been discarded its entry is effectively removed from the map". But unless there is another thread that occasionally removes such Map.Entry entries, won't the value objects be strongly referenced by the map? But since there is no such thread running, only the get method invocations can remove such entries - one at a time. I almost always use WeakHashMap<K, WeakReference<V>> for that reason. Why would they not have made that the default behavior - values as weak references too? Reference queues are used to automatically remove entries. http://docs.oracle.com/javase

WeakHashMap vs HashMap

China☆狼群 提交于 2019-12-03 05:13:32
问题 In the following code example when keys are set to null and System.gc() is called, the WeakHashMap loses all mappings and is emptied. class WeakHashMapExample { public static void main(String[] args) { Key k1 = new Key("Hello"); Key k2 = new Key("World"); Key k3 = new Key("Java"); Key k4 = new Key("Programming"); Map<Key, String> wm = new WeakHashMap<Key, String>(); wm.put(k1, "Hello"); wm.put(k2, "World"); wm.put(k3, "Java"); wm.put(k4, "Programming"); k1=null; k2=null; k3=null; k4=null;

WeakHashMap vs HashMap

半城伤御伤魂 提交于 2019-12-02 19:38:42
In the following code example when keys are set to null and System.gc() is called, the WeakHashMap loses all mappings and is emptied. class WeakHashMapExample { public static void main(String[] args) { Key k1 = new Key("Hello"); Key k2 = new Key("World"); Key k3 = new Key("Java"); Key k4 = new Key("Programming"); Map<Key, String> wm = new WeakHashMap<Key, String>(); wm.put(k1, "Hello"); wm.put(k2, "World"); wm.put(k3, "Java"); wm.put(k4, "Programming"); k1=null; k2=null; k3=null; k4=null; System.gc(); System.out.println("Weak Hash Map :"+wm.toString()); } } class Key{ private String key;

How WeakHashMap works under the hood

眉间皱痕 提交于 2019-12-02 19:32:40
问题 I invesigate WeakHashMap ource code to have more knowledge about WeakReference I have found that entry looks like this: private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { V value; final int hash; Entry<K,V> next; /** * Creates new entry. */ Entry(Object key, V value, ReferenceQueue<Object> queue, int hash, Entry<K,V> next) { super(key, queue); this.value = value; this.hash = hash; this.next = next; } ... Thus when we create new entry we invoke super(key,

How WeakHashMap works under the hood

雨燕双飞 提交于 2019-12-02 10:53:06
I invesigate WeakHashMap ource code to have more knowledge about WeakReference I have found that entry looks like this: private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> { V value; final int hash; Entry<K,V> next; /** * Creates new entry. */ Entry(Object key, V value, ReferenceQueue<Object> queue, int hash, Entry<K,V> next) { super(key, queue); this.value = value; this.hash = hash; this.next = next; } ... Thus when we create new entry we invoke super(key, queue); . It is WeakReference constructor. As far I understand after object will be collected by GC

How does a value in an entry in the WeakHashMap gets garbage collected when the actual object is garbage collected?

别说谁变了你拦得住时间么 提交于 2019-11-30 19:44:29
问题 First of all I would like to clarify my understanding of the WeakReference as the following question depends on the same. static void test() { Person p = new Person(); WeakReference<Person> person = new WeakReference<>(p); p = null; System.gc(); System.out.println(person.get()); System.out.println(person); } static class Person { String name; } static class PersonMetadata { String someData; public PersonMetadata(String met) { someData = met; } } The output of the above code is null java.lang

Why WeakHashMap holds strong reference to value after GC?

心已入冬 提交于 2019-11-30 14:20:06
Key object in WeakHashMap became weakly reachable. And map should be remove the entry after GC. But a strong reference to the value object remains. Why? The same behavior is observed with guava weakkeys map. Expected output: ... refKey.get = null refValue.get = null But I get output: map.keys = [] map.values = [] map.size = 0 refKey.get = null refValue.get = (123) Code: import java.lang.ref.WeakReference; import java.util.Map; import java.util.WeakHashMap; import com.google.common.collect.MapMaker; public class Test { static class Number { final int number; public Number(int number) { this

Why WeakHashMap holds strong reference to value after GC?

北慕城南 提交于 2019-11-29 20:37:09
问题 Key object in WeakHashMap became weakly reachable. And map should be remove the entry after GC. But a strong reference to the value object remains. Why? The same behavior is observed with guava weakkeys map. Expected output: ... refKey.get = null refValue.get = null But I get output: map.keys = [] map.values = [] map.size = 0 refKey.get = null refValue.get = (123) Code: import java.lang.ref.WeakReference; import java.util.Map; import java.util.WeakHashMap; import com.google.common.collect

Is WeakHashMap ever-growing, or does it clear out the garbage keys?

冷暖自知 提交于 2019-11-29 08:38:17
I am trying to use WeakHashMap as a concurrent Set of weak references. this.subscribers = Collections.synchronizedSet( Collections.newSetFromMap( new WeakHashMap <>() ) ); When an element goes to garbage-collection, my set continues to report it as a part of the collection. So it seems the map is ever-growing. The documentation says: When a key has been discarded its entry is effectively removed from the map,… But that does not seem to be the case in practice. Is there ever a point at which the WeakHashMap clears out the detritus? Yes, keys cleared after garbage is actually collected Yes,