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
907
votes
12 answers

What's the difference between SoftReference and WeakReference in Java?

What's the difference between java.lang.ref.WeakReference and java.lang.ref.SoftReference ?
driekken
  • 11,443
  • 4
  • 19
  • 10
212
votes
7 answers

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

I have read this article about different types of references in Java (strong, soft, weak, phantom), but I don't really understand it. What is the difference between these reference types, and when would each type be used?
user1204873
82
votes
4 answers

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

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…
69
votes
7 answers

Is there a SoftHashMap in Java?

I know there is a WeakHashMap in java.util, but since it uses WeakReferences for everything, which is only referenced by this Map, referenced objects will get lost on the next GC cycle. So it's nearly useless if you want to cache random data, which…
tigger
  • 1,856
  • 3
  • 18
  • 23
28
votes
4 answers

Using Java's ReferenceQueue

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,…
bgroenks
  • 1,859
  • 5
  • 34
  • 63
26
votes
12 answers

How do I efficiently cache objects in Java using available RAM?

I need to cache objects in Java using a proportion of whatever RAM is available. I'm aware that others have asked this question, but none of the responses meet my requirements. My requirements are: Simple and lightweight Not dramatically slower…
sanity
  • 35,347
  • 40
  • 135
  • 226
21
votes
10 answers

How to cause soft references to be cleared in Java?

I have a cache which has soft references to the cached objects. I am trying to write a functional test for behavior of classes which use the cache specifically for what happens when the cached objects are cleared. The problem is: I can't seem to…
Epaga
  • 38,231
  • 58
  • 157
  • 245
20
votes
5 answers

How to make the java system release Soft References?

I'm going to use a SoftReference-based cache (a pretty simple thing by itself). However, I've came across a problem when writing a test for it. The objective of the test is to check if the cache does request the previously cached object from the…
JBM
  • 2,930
  • 2
  • 24
  • 28
19
votes
3 answers

Are C# weak references in fact soft?

The basic difference is that weak references are supposed to be claimed on each run of the GC (keep memory footprint low) while soft references ought to be kept in memory until the GC actually requires memory (they try to expand lifetime but may…
mafu
  • 31,798
  • 42
  • 154
  • 247
19
votes
9 answers

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

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…
Nathan Evans
17
votes
5 answers

How are SoftReferences collected by JVMs in practice?

I have two separate caches running in a JVM (one controlled by a third party library) each using soft references. I would prefer for the JVM to clear out my controlled cache before the one controlled by the library. The SoftReference javadoc…
Dave L.
  • 43,907
  • 11
  • 63
  • 62
15
votes
4 answers

What is a use case for a soft reference in Java?

What is a use case for a soft reference in Java? Would it be useful to garbage collect non-critical items when a JVM has run out of memory in order to free up enough resources to perhaps dump critical information before shutting down the JVM? Are…
benstpierre
  • 32,833
  • 51
  • 177
  • 288
12
votes
1 answer

Is there a way to FORCE weak and/or soft referenced objects to be GC'd in Java?

Here's my use case. We are trying to narrow down a potential memory leak in an application, and we are using a memory analysis tool to snapshot the heap so we can look for object instances and references. (In case it helps, we're using…
11
votes
2 answers

Can "soft references" exist in Python?

In other languages (e.g. Java), object references can be Strong, Weak, Soft or Phantom (http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html). In Python, references are Strong by default and the WeakRef module allows weak…
afaulconbridge
  • 1,107
  • 9
  • 21
11
votes
5 answers

SoftReference gets garbage collected too early

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…
Wroclai
  • 26,835
  • 7
  • 76
  • 67
1
2 3 4 5 6