有人可以向我解释一下什么时候使用 MapMaker 或 WeakHashMaps 有用吗?

发布于 2024-09-17 01:59:03 字数 943 浏览 10 评论 0原文

我读到很多人都非常喜欢 MapMaker Google Guava(集合),但是我看不到它有任何好的用途。

我已阅读 javadoc,它说它的行为类似于 ConcurrentHashMap。它还说 new MapMaker().weakKeys().makeMap() 几乎总是可以用作 WeakHashMap

但是,阅读 ConcurrentHashMapWeakHashMap 让我想知道什么时候使用它有用?在我看来,你不能保证你在地图中放入的任何内容都会在那里,还是我误解了?

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 in the map will be there, or have I misunderstood?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云醉月微眠 2024-09-24 02:03:56

ConcurrentHashMap 是一个可以在多线程环境中安全使用的Map。它比常规 Map 的同步版本更好,因为并发意味着不同的线程通常可以无阻塞地访问此映射。

ConcurrentHashMap is a Map which may be safely used in multi-threading environment. It is better than synchronized version of regular Map because concurrency means that different threads are often available to access this map without blocking.

夏の忆 2024-09-24 02:03:07

当有人(除了地图之外)引用该条目时,WeakHashmap 条目将保留在地图中。如果除了映射之外没有其他人保留对该条目的引用,则可以在下一次 GC 运行时删除该条目。

A WeakHashmap entry will be kept in the map while someone (other than the map) is referencing the entry. If nobody else thant the map is keeping a reference on the entry, then the entry can be removed on a next GC run.

无悔心 2024-09-24 02:02:19

...这就是它的重点。如果您不想(或无力)将对象无限期地保留在内存中,那么弱引用很有用。考虑以下用例:您需要将信息与类相关联。现在,由于您正在一个可能会重新加载类的环境中运行(例如 Tomcat 或 OSGi 环境),因此您希望垃圾收集器能够在认为安全时尽快回收旧版本的类。

实现此目的的初步尝试可能看起来像

class ClassAssoc {
    private final IdentityHashMap<Class<?>,MyMetaData> cache = new ...;
}

这里的问题是:这会将所有类永远保留在 cache 成员中(或者至少,除非它们被手动删除),迫使垃圾收集器保留无限期地使用它们,包括从类引用的所有内容(静态成员值、类加载器信息等)

通过使用弱引用,垃圾收集器可以在不存在对该类的其他引用(通常是实例)时立即回收该类的旧版本。另一方面:只要存在此类引用,就保证该值也可以从弱引用对象访问,因此是缓存表中的有效键。

将并发性和其他暴行添加到图片中,您就可以选择 MapMaker 还提供的内容...

...and that's somewhat the point of it. Weak references are useful, if you don't want to (or cannot afford to) retain an object indefinetly in memory. Consider the following use case: you need to associate information with classes. Now, since you are running in an environment, where classes might get reloaded (say, a Tomcat, or OSGi environment), you want the garbage collector to be able to reclaim old versions of a class as soon as it deems safe to do so.

An initial attempt to implement this, might look like

class ClassAssoc {
    private final IdentityHashMap<Class<?>,MyMetaData> cache = new ...;
}

The problem here is: this would keep all classes in the cache member forever (or at least, unless they are manually removed), forcing the garbage collector to retain them indefinitly, including everything referenced from the class (static member values, class loader information, ...)

By using weak references, the garbage collector can reclaim old version of the class as soon as no other references to it (usually instances) exist. On the other hand: as long as such references exist, the value is guaranteed to be also reachable from the weak reference object, and thus, is a valid key in the cache table.

Add concurrency and other atrocities to the picture, and you are at what MapMaker optionally also provides...

黯然#的苍凉 2024-09-24 02:01:26

MapMaker 的特点是,您构建的地图类型有很多选项,这使得这些地图能够满足多种用途。

  • Dirk 给出了弱键使用的一个很好的例子。
  • 软值对于缓存很有用,因为您可以在映射中缓存值,而不必担心内存不足,因为如果需要内存,系统可以自由地从缓存中逐出条目。
  • 您可以选择让条目在一段时间后过期。这对于缓存也很有用,因为您可能希望在执行昂贵的更新操作之前将某些数据缓存特定的时间段。
  • 我最喜欢的事情之一是制作计算地图。计算映射使用 Function 自动检索与给定键关联的值(如果映射中尚不存在该值)。这与软值和/或过期时间很好地结合在一起。在映射驱逐条目后(由于内存需求或过期),下次请求与该键关联的值时,它将自动检索并再次缓存在映射中。

The thing about MapMaker is that there are many options for the kind of map you build, which enables those maps to serve many purposes.

  • Dirk gives a good example of a use for weak keys.
  • Soft values are useful for caching, as you can cache values in the map without worrying about running out of memory since the system is free to evict entries from the cache if it needs memory.
  • You can choose to have entries expire after a certain amount of time. This is also useful for caching, since you may want certain data cached for a specific period of time before doing an expensive operation to update it.
  • One of my favorite things is making a computing map. A computing map uses a Function<K, V> to automatically retrieve the value associated with a given key if it isn't already in the map. This combines well with soft values and/or expiration times. After an entry is evicted by the map (due to memory demand or expiration), the next time the value associated with that key is requested it will automatically be retrieved and cached in the map once more.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文