android - java - 带有 ArrayList 的 WeakReferences?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22555374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:27:55  来源:igfitidea点击:

android - java - WeakReferences with an ArrayList?

javaandroidarraylistgarbage-collectionweak-references

提问by codingNewb

I know that with a WeakReference, if I make a WeakReferenceto something that unless there's a direct reference to it that it will be Garbage Collected with the next GC cycle. My question becomes, what if I make an ArrayListof WeakReferences?

我知道使用WeakReference, 如果我WeakReference对某些东西做 a ,除非有直接引用它,否则它将在下一个 GC 循环中被垃圾收集。我的问题变成了,如果我做一个ArrayListof WeakReferences 怎么办?

For example:

例如:

ArrayList<WeakReference<String>> exArrayList;
exArrayList = new ArrayList<WeakReference<String>>();
exArrayList.add(new WeakReference<String>("Hello"));

I can now access the data with exArrayList.get(0).get().

我现在可以使用exArrayList.get(0).get().

My question becomes: This is WeakReferencedata, will the data located at exArrayList.get(0)be GC'd with the next GC cycle? (even IF I don't make another direct reference to it) or will this particular reference stick around until the arraylistis emptied? (eg: exArrayList.clear();).

我的问题变成:这是WeakReference数据,位于 的数据会exArrayList.get(0)在下一个 GC 周期中被 GC 处理吗?(即使我没有直接引用它)或者这个特定的引用会一直存在直到arraylist清空?(例如:)exArrayList.clear();

If this is a duplicate I haven't found it with my keywords in google.

如果这是重复的,我还没有在 google 中用我的关键字找到它。

采纳答案by Evgeniy Dorofeev

  1. exArrayList.add(new WeakReference<String>("Hello"));is a bad example because Stringliterals are never GC-ed

  2. if it were e.g. exArrayList.add(new WeakReference<Object>(new Object()));then after a GC the object would be GC-ed, but exArrayList.get(0)would still return WeakReference, though exArrayList.get(0).get()would return null

  1. exArrayList.add(new WeakReference<String>("Hello"));是一个不好的例子,因为String文字永远不会被垃圾回收

  2. 如果是,exArrayList.add(new WeakReference<Object>(new Object()));那么在 GC 之后,对象将被 GC 处理,但exArrayList.get(0)仍会返回WeakReference,尽管exArrayList.get(0).get()会返回null

回答by SJuan76

The data at exArrayList.get(0)is the WeakReference. It is not by itself a weak reference, so it will not be collected...

处的数据exArrayList.get(0)WeakReference。它本身不是弱引用,因此不会被收集......

BUT the object referenced by exArrayList.get(0)is weakly referenced, so it mightbe GCed at any time (of course, that requires that there are no strongreferences to that object).

但是被引用的对象exArrayList.get(0)是弱引用的,所以它可能随时被GCed(当然,这要求没有对该对象的引用)。

So

所以

data.get(0)won't become null, but data.get(0).get()might become.

data.get(0)不会变成null,但data.get(0).get()可能会变成。

In other words, the list does not references the weak referenced object but the weak reference itself.

换句话说,列表不引用弱引用对象,而是引用弱引用本身。

回答by Stefan Reich

This is a bad idea as the other posters explained above (reference objects not freed). Use a WeakHashMap with the objects as keys and some dummy values ("" or Boolean.TRUE or similar).

正如上面解释的其他海报一样,这是一个坏主意(参考对象未释放)。使用 Wea​​kHashMap 将对象作为键和一些虚拟值("" 或 Boolean.TRUE 或类似的)。