我什么时候应该使用MapMaker类的weakValues()?

时间:2021-08-26 20:32:15

When a entry in a map has weak key reference, the entry will be removed at the next garbage collection, right?

当地图中的条目具有弱密钥引用时,该条目将在下一次垃圾回收时被删除,对吧?

I can understand that the MapMaker class provides the weakKeys method. But I am confused with the weakValue(). when should I use weakValue or softValue in MapMaker?

我可以理解MapMaker类提供了weakKeys方法。但我对weakValue()感到困惑。我何时应该在MapMaker中使用weakValue或softValue?

1 个解决方案

#1


3  

You'd use weakValues() when you want entries whose values are weakly reachable to be garbage collected. For an example of when this might be useful... say you have a class that allows users to add objects to it and stores them as values in a Map for whatever reason. This class is typically used as a singleton, so it'll stick around the whole time your application is running. However, the objects the user adds to it aren't necessarily so long-lived. The application will be done with them long before it finishes. You don't want the user to have to manually remove these objects from your class when it is finished with them, but you don't want a memory leak by keeping references to them in your class forever (in other words garbage collection should just work like normal, ignoring your class). The solution is to give the map weakValues() and everything will work as you want.

当您希望将值很弱的条目进行垃圾回收时,可以使用weakValues()。有关何时可能有用的示例...假设您有一个类,允许用户向其添加对象,并将其作为值存储在Map中,无论出于何种原因。这个类通常用作单例,因此它会在应用程序运行的整个过程中保持不变。但是,用户添加的对象不一定是如此长寿。应用程序将在完成之前很久就完成。您不希望用户在完成它们时必须从类中手动删除这些对象,但是您不希望通过在类中永久引用它们而导致内存泄漏(换句话说,垃圾收集应该只是工作正常,无视你的班级)。解决方案是给地图weakValues(),一切都会按你的意愿工作。

softValues() is good for caching... if you have a Map<Integer, Foo> and you want entries to to be removable in response to memory demand, you'd want to use it. You wouldn't want to use weakKeys() or softKeys() because they both use == identity, which would cause problems for you (wouldn't be able to get a value with key 300 out because the key you pass in probably wouldn't == the key in the map).

softValues()适用于缓存......如果你有一个Map ,并且你希望条目可以被移除以响应内存需求,那么你想要使用它。您不希望使用weakKeys()或softKeys(),因为它们都使用== identity,这会给您带来问题(因为您传入的密钥可能不会因为密钥300输出而无法获取值't ==地图中的键)。 ,foo>

#1


3  

You'd use weakValues() when you want entries whose values are weakly reachable to be garbage collected. For an example of when this might be useful... say you have a class that allows users to add objects to it and stores them as values in a Map for whatever reason. This class is typically used as a singleton, so it'll stick around the whole time your application is running. However, the objects the user adds to it aren't necessarily so long-lived. The application will be done with them long before it finishes. You don't want the user to have to manually remove these objects from your class when it is finished with them, but you don't want a memory leak by keeping references to them in your class forever (in other words garbage collection should just work like normal, ignoring your class). The solution is to give the map weakValues() and everything will work as you want.

当您希望将值很弱的条目进行垃圾回收时,可以使用weakValues()。有关何时可能有用的示例...假设您有一个类,允许用户向其添加对象,并将其作为值存储在Map中,无论出于何种原因。这个类通常用作单例,因此它会在应用程序运行的整个过程中保持不变。但是,用户添加的对象不一定是如此长寿。应用程序将在完成之前很久就完成。您不希望用户在完成它们时必须从类中手动删除这些对象,但是您不希望通过在类中永久引用它们而导致内存泄漏(换句话说,垃圾收集应该只是工作正常,无视你的班级)。解决方案是给地图weakValues(),一切都会按你的意愿工作。

softValues() is good for caching... if you have a Map<Integer, Foo> and you want entries to to be removable in response to memory demand, you'd want to use it. You wouldn't want to use weakKeys() or softKeys() because they both use == identity, which would cause problems for you (wouldn't be able to get a value with key 300 out because the key you pass in probably wouldn't == the key in the map).

softValues()适用于缓存......如果你有一个Map ,并且你希望条目可以被移除以响应内存需求,那么你想要使用它。您不希望使用weakKeys()或softKeys(),因为它们都使用== identity,这会给您带来问题(因为您传入的密钥可能不会因为密钥300输出而无法获取值't ==地图中的键)。 ,foo>