weakhashmap

深入理解WeakHashmap

和自甴很熟 提交于 2020-03-24 16:24:56
3 月,跳不动了?>>> 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://mikewang.blog.51cto.com/3826268/880775 WeakHashmap (一) 查看 API 文档, WeakHashmap 要点如下: 1. 以弱键 实现的基于哈希表的 Map。在 WeakHashMap 中,当某个键 不再正常使用 时,将自动移除其条目。更精确地说,对于一个给定的键,其映射的存在并不阻止垃圾回收器对该键的丢弃,这就使该键成为可终止的,被终止,然后被回收。丢弃某个键时,其条目从映射中有效地移除 2. WeakHashMap 类的行为部分取决于垃圾回收器的动作。因为垃圾回收器在任何时候都可能丢弃键,WeakHashMap 就像是一个被悄悄移除条目的未知线程。特别地,即使对 WeakHashMap 实例进行同步,并且没有调用任何赋值方法,在一段时间后 size 方法也可能返回较小的值,对于 isEmpty 方法,返回 false,然后返回true,对于给定的键,containsKey 方法返回 true 然后返回 false,对于给定的键,get 方法返回一个值,但接着返回 null,对于以前出现在映射中的键,put 方法返回 null,而 remove 方法返回 false,对于键 set、值

Java弱引用与WeakHashMap

徘徊边缘 提交于 2020-03-09 18:58:52
在《Effective Java 2nd Edition》中,第6条“消除过期的对象引用”提到,虽然Java有 垃圾回收机制,但是只要是自己管理的内存,就应该警惕内存泄露的问题,例如的对象池、缓存中的过期对象都有可能引发内存泄露的问题。书中还提到可以用 WeakHashMap来作为缓存的容器可以有效解决这一问题。之前也确实遇到过类似问题,但是没有接触过“弱引用”相关的问题,于是查阅了一些资料。 《Java 理论与实践: 用弱引用堵住内存泄漏》一文也指出了使用全局的Map作为缓存容器时发生的内存泄露问题,介绍了如何使用hprof工具来找出内存泄露,并分析了如何使用 弱引用来防止内存泄露,还分析了WeakHashMap的关键代码,非常有参考价值。但是这篇文章遗漏了几个很重要的需要注意的地方,也缺少一段实验代 码,本文将会做出适当补充。 1、四种引用 从JDK1.2版本开始,把对象的引用分为四种级别,从而使程序能更加灵活的控制对象的生命周期。这四种级别由高到低依次为:强引用、软引用、弱引用和虚引用。 http://docs.oracle.com/javase/7/docs/api/java/lang/ref/Reference.html 强引用:平时我们编程的时候例如:Object object=new Object();那object就是一个强引用了。如果一个对象具有强引用

Usage of WeakHashMap? [duplicate]

天大地大妈咪最大 提交于 2020-01-01 10:04:49
问题 This question already has answers here : When would you use a WeakHashMap or a WeakReference? (10 answers) Closed last year . WeakHashMap is an implementation of Map interface where the memory of the value object can be reclaimed by Grabage Collector if the corresponding key is no longer referred by any section of program. So if key is no longer used in program. its Entry object will be garbage collected irrespective of its usage. Its clear till here This is different from HashMap where the

Are keySet entries of a WeakHashMap never null?

六眼飞鱼酱① 提交于 2019-12-23 07:29:12
问题 If I iterate over the key set of a WeakHashMap, do I need to check for null values? WeakHashMap<MyObject, WeakReference<MyObject>> hm = new WeakHashMap<MyObject, WeakReference<MyObject>>(); for ( MyObject item : hm.keySet() ) { if ( item != null ) { // <- Is this test necessary? // Do something... } } In other words, can elements of the WeakHashMap be collected while I am iterating over them? EDIT For the sake of this question, one can assume that no null entries is added in the hash map. 回答1

Reduce Memory Usage With WeakHashMap

拥有回忆 提交于 2019-12-23 02:38:23
问题 In the Javadoc of WeakHashMap.html, it said "Each key object in a WeakHashMap is stored indirectly as the referent of a weak reference. Therefore a key will automatically be removed only after the weak references to it, both inside and outside of the map, have been cleared by the garbage collector." And then Note that a value object may refer indirectly to its key via the WeakHashMap itself; that is, a value object may strongly refer to some other key object whose associated value object, in

When will Java WeakHashMap clean null key?

心不动则不痛 提交于 2019-12-12 18:28:18
问题 In the code below nameRef.get() is null , after name = null and System.gc() . import java.lang.ref.WeakReference; public class Main { public static void main(String[] args) { String name = new String("ltt"); WeakReference<String> nameRef = new WeakReference<>(name); System.out.println(nameRef.get()); // ltt name = null; System.gc(); System.out.println(nameRef.get()); // null } } WeakHashMap is based on WeakReference. At last, I think map.size() will be 0. In fact, it's 1. import java.util

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

主宰稳场 提交于 2019-12-12 09:37:42
问题 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

WeakHashMap and strongly referenced value

北战南征 提交于 2019-12-09 05:39:56
问题 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

spring security, how to expire all sessions of a user

痴心易碎 提交于 2019-12-08 04:01:58
问题 I have to solve the following scenario, in a Spring Security 3.2.5-RELEASE with Spring Core 4.1.2-RELEASE application running Java 1.7 on wildfly 8.1. user 'bob' logs in and Admin deletes 'bob' if 'bob' logs out, he can't log in. again but he`s current session remains active. i want to kick 'bob' out //this doesn't work for (final SessionInformation session : sessionRegistry.getAllSessions(user, true)) { session.expireNow(); } 回答1: add application event listener to track

Can someone explain to me when it is useful to use MapMaker or WeakHashMaps?

老子叫甜甜 提交于 2019-12-07 06:33:37
问题 I have read many people really like the MapMaker of Google Guava (Collections), however I cannot see any good uses of it. I have read the javadoc, and it says that it behaves like ConcurrentHashMap. It also says new MapMaker().weakKeys().makeMap() can almost always be used as a drop-in replacement for WeakHashMap. However, reading the javadocs of ConcurrentHashMap and WeakHashMap makes me wonder when it is useful to use it? It seems to me that you cannot have a guarantee that whatever you put