soft-references

Using Java's ReferenceQueue

半世苍凉 提交于 2019-11-27 06:49:54
Do SoftReference and WeakReference really only help when created as instance variables? Is there any benefit to using them in method scope? The other big part is ReferenceQueue . Besides being able to track which references are determined garbage, can Reference.enqueue() be used to forcibly register an object for garbage collection? For example, would it be worth to create a method that takes some heavy memory resources (held by strong references) in an object and creating References to enqueue them? Object bigObject; public void dispose() { ReferenceQueue<Object> queue = new ReferenceQueue

Why doesn't .NET have a SoftReference as well as a WeakReference, like Java?

爷,独闯天下 提交于 2019-11-27 06:43:46
问题 I really love WeakReference's. But I wish there was a way to tell the CLR how much (say, on a scale of 1 to 5) how weak you consider the reference to be. That would be brilliant. Java has SoftReference, WeakReference and I believe also a third type called a "phantom reference". That's 3 levels right there which the GC has a different behaviour algorithm for when deciding if that object gets the chop. I am thinking of subclassing .NET's WeakReference (luckily and slightly bizzarely it isn't

Understanding Java's Reference classes: SoftReference, WeakReference, and PhantomReference

情到浓时终转凉″ 提交于 2019-11-27 02:30:00
Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference , but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or PhantomHashMap ? And if I use the following code... WeakReference<String> ref = new WeakReference<String>("Hello!"); if (ref != null) { // ref can get collected at any time... System.gc(); // Let's assume ref gets collected here. System.out.println(ref.get()); // Now what?! } ...what happens? Do I have to check if ref is null before every statement (this

SoftReference gets garbage collected too early

左心房为你撑大大i 提交于 2019-11-27 01:25:27
问题 I'm on my way with implementing a caching mechanism for my Android application. I use SoftReference , like many examples I've found. The problem is, when I scroll up or down in my ListView , the most of the images are already cleared. I can see in LogCat that my application is garbage collected everytime the application loads new images. That means that the most of the non-visible images in the ListView are gone. So, everytime I scroll back to an earlier position (where I really downloaded

Java: difference between strong/soft/weak/phantom reference

好久不见. 提交于 2019-11-26 19:17:32
I have read this article about the topic, but I don't really understand it. Please give me some advice along with examples when describing the concepts. Paolo Maresca Java provides two different types/classes of Reference Objects : strong and weak . Weak Reference Objects can be further divided into soft and phantom . Let's go point by point. Strong Reference Object StringBuilder builder = new StringBuilder(); This is the default type/class of Reference Object, if not differently specified: builder is a strong Reference Object. This kind of reference makes the referenced object not eligible

Using Java&#39;s ReferenceQueue

◇◆丶佛笑我妖孽 提交于 2019-11-26 12:11:46
问题 Do SoftReference and WeakReference really only help when created as instance variables? Is there any benefit to using them in method scope? The other big part is ReferenceQueue . Besides being able to track which references are determined garbage, can Reference.enqueue() be used to forcibly register an object for garbage collection? For example, would it be worth to create a method that takes some heavy memory resources (held by strong references) in an object and creating References to

Understanding Java&#39;s Reference classes: SoftReference, WeakReference, and PhantomReference

纵然是瞬间 提交于 2019-11-26 10:07:48
问题 Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? SoftReference > WeakReference > PhantomReference , but when would I use each one? Why is there a WeakHashMap but no SoftHashMap or PhantomHashMap ? And if I use the following code... WeakReference<String> ref = new WeakReference<String>(\"Hello!\"); if (ref != null) { // ref can get collected at any time... System.gc(); // Let\'s assume ref gets collected here. System.out.println(ref

Java: difference between strong/soft/weak/phantom reference

醉酒当歌 提交于 2019-11-26 06:09:56
问题 I have read this article about the topic, but I don\'t really understand it. Please give me some advice along with examples when describing the concepts. 回答1: Java provides two different types/classes of Reference Objects : strong and weak . Weak Reference Objects can be further divided into soft and phantom . Let's go point by point. Strong Reference Object StringBuilder builder = new StringBuilder(); This is the default type/class of Reference Object, if not differently specified: builder

What&#39;s the difference between SoftReference and WeakReference in Java?

一世执手 提交于 2019-11-26 01:29:16
问题 What\'s the difference between java.lang.ref.WeakReference and java.lang.ref.SoftReference ? 回答1: From Understanding Weak References, by Ethan Nicholas: Weak references A weak reference , simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself. You create a weak reference like this: WeakReference weakWidget = new