Questions tagged [soft-references]

A soft reference is very similar to a weak reference, except that it is less eager to throw away the object to which it refers. An object which is only weakly reachable (the strongest references to it are WeakReferences) will usually be discarded at the next garbage collection cycle, but an object which is softly reachable will generally stick around for a while.

A Soft Reference is a reference that does not protect the referenced object from collection by a garbage collector. An object referenced only by soft references is considered unreachable (or "weakly reachable") and so may be collected at any time.

The main difference between Soft and Weak references is that while both will allow the object to be reclaimed Soft references say "try to hang onto this unless we need the memory" whereas Weak references says "throw this away whenever you like". An object which is only weakly reachable (the strongest references to it are WeakReferences) will usually be discarded at the next garbage collection cycle, whereas objects with a SoftReference may be discarded, but will usually be kept.

A soft reference can be used to create new strong references, which then means that until those strong references go out of scope the object will no longer be eligible for garbage collection.

Soft references are used in areas such as caching, where you want to keep a reference to an object for re-use but allow the system to reclaim it if memory grows low.

Some garbage-collected languages feature or support various levels of weak references, such as Java, C#, Python, Perl and Lisp. Java also offers Weak References and Phantom References which offer variations on the same functionality.

88 questions
10
votes
3 answers

Android: the GC doesn't respect SoftReferences?

It seams that Dalvik's garbage collector doesn't respect SoftReferences and removes them as soon as possible, just like WeakReferences. I'm not 100% sure yet, but despite the fact that there is still ~3MB of free memory my SoftReferences get cleared…
JBM
  • 2,930
  • 2
  • 24
  • 28
10
votes
4 answers

Gracefully finalizing the SoftReference referent

I am using a search library which advises keeping search handle object open for this can benefit query cache. Over the time I have observed that the cache tends to get bloated (few hundred megs and keeps growing) and OOMs started to kick in. There…
mindas
  • 26,463
  • 15
  • 97
  • 154
8
votes
4 answers

When to use Weak and Phantom references in Java

I read many articles, but I don't understand - where do I need to use Weak and Phantom references in practice? Soft references - is a good choice for cache, as I understand. But weak and phantom, I don't know when to use. Please provide examples of…
7
votes
2 answers

What are the "practical consequences" of using soft references?

Per the documentation for Guava's MapMaker.softValues(): Warning: in most circumstances it is better to set a per-cache maximum size instead of using soft references. You should only use this method if you are well familiar with the practical…
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
7
votes
4 answers

Is there a SoftHashMap in Scala?

I'm aware of this question for java, but none of those implementations seem to play well with scala.collection.JavaConversions. I'm looking for something simple (e.g. single file, not a whole library) that implements SoftHashMap such that it plays…
dsg
  • 12,924
  • 21
  • 67
  • 111
7
votes
5 answers

Soft (not: weak) references in C++ - Is it possible? Is there an implementation?

In C++ I'm using boost::shared_ptr and boost::weak_ptr to automatically delete objects that are no longer needed. I know these work with reference counting. In Java, memory is managed by a garbage collector, which consideres the built-in object…
Lena Schimmel
  • 7,203
  • 5
  • 43
  • 58
7
votes
1 answer

Understanding Phantom reference vs weak reference with respect to Reference queue

As per the link https://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html, PhantomReferences are enqueued only when the object is physically removed from memory and WeakReferences are enqueued before finalization or garbage…
Anand
  • 20,708
  • 48
  • 131
  • 198
6
votes
5 answers

Equivalent to SoftReference in .net?

I am familiar with WeakReference, but I am looking for a reference type that is cleared only when memory is low, not simply every time when the gc runs (just like Java's SoftReference). I'm looking for a way to implement a memory-sensitive cache.
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
6
votes
1 answer

Java SoftReference guarantee failing

The JDK 7 documentation has this to say about a SoftReference: "All soft references to softly-reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError." However, in my test program, I'm seeing…
5
votes
1 answer

Android: Bitmaps, SoftReferences, and OOMs?

I have a series of views in a vertical LinearLayout. Each view generates and draws a Bitmap, when scrolled to. For performance reasons, I would rather not generate the Bitmap each time onDraw() is called, but for memory reasons I can not keep hard…
ab11
  • 19,770
  • 42
  • 120
  • 207
5
votes
1 answer

Is there any way to make a soft reference or Pointer-like objects using Numpy arrays?

I was wondering whether there is a way to refer data from many different arrays to one array, but without copying it. Example: import numpy as np a = np.array([2,3,4,5,6]) b = np.array([5,6,7,8]) c = np.ndarray([len(a)+len(b)]) offset =…
Alejandro
  • 3,263
  • 2
  • 22
  • 38
4
votes
3 answers

Soft reference LinkedHashMap in Java?

Is there softreference-based LinkedHashMap in Java? If no, has anyone got a snippet of code that I can probably reuse? I promise to reference it correctly. Thanks.
His
  • 5,891
  • 15
  • 61
  • 82
4
votes
1 answer

Bitmap Cache (SoftReference, Hard) on Lazy List does not seem to work properly - Android

I have read several topics on lazy list loading in stackoverflow and I am trying to understand how to work on the different cache levels in android. As mentioned here: Lazy load of images in ListView I used the Multithreading For Performance, a…
andreasv
  • 689
  • 3
  • 11
  • 26
4
votes
1 answer

Will all the soft references in java be cleared in one single shot?

I read this article and now I'm clear about what is the basic difference between weak and soft references. I also understand that unlike weak references (which will cause the object to be collected in the next GC cycle if no strong references are…
Lavish Kothari
  • 2,211
  • 21
  • 29
4
votes
1 answer

Uses of different reference types in Java

I've recently been playing around with soft, weak and phantom reference types in Java and have been wondering if there's any uses out there for them that I haven't come across. I've used them in the past for various things and they've always fallen…
Michael Berry
  • 70,193
  • 21
  • 157
  • 216