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
2
votes
1 answer

Implementing a Write-Back Cache in Java

I trying to implement a write-back cache. I'm trying to use soft referenes, but I'm having troubles performing the post-mortum write-back because the reference is cleared before it's added to the gcQueue and thus I don't have access to the referent…
Jim
  • 2,111
  • 4
  • 25
  • 34
2
votes
3 answers

Will GC collect an object referred to by a SoftReference and a WeakReference?

I have a cache built from a Map to SoftReferences. When they are added they get put into another queue to be lazily compressed down via gzip or some such. My idea is this: I want to have WeakReferences to the objects in the compress queue, so that…
gub
  • 5,079
  • 3
  • 28
  • 33
2
votes
2 answers

Usage of SoftReference in Map?

I see below implementation of LRU cache in a legacy project where I have a question on usage of SoftReference for value object but not for key object. Here is the implementation public class LRUCacheImpl implements LRUCache { //…
user3198603
  • 5,528
  • 13
  • 65
  • 125
2
votes
2 answers

SoftReference not garbage collected when out of memory

I am testing SoftReference for a cache implementation and I found a strange behaviour : I have a setName(String name) method which sets the name of a Graph object through a SoftReference : public void setName(String newName) { getData().name =…
zelus
  • 143
  • 8
2
votes
1 answer

Using LruCache and avoid OutOfMemoryException

There's an activity in my app whose showing around 1000 very small sized bitmaps (Around 20kb each bitmap). After it loads some of the bitmaps, there's an OutOfMemoryException. I was first reading about SoftReference and it looked like it will solve…
idish
  • 3,190
  • 12
  • 53
  • 85
2
votes
0 answers

weakreferences of fragments on Orientation Change using FragmentStatePagerAdapter -Android

Im keeping track of fragments in a fragmentstatepageradapter using weakreferences to the fragments. I found it cheaper then making a vector to keep track of the actual fragments. So then i could call a function called getFragmentt(2) for example…
2
votes
3 answers

A cache which knows about reachability

I'd like a cache with some maximum retaining capacity of N. I'm allowing it to hold up to N objects which would otherwise be eligible for GC. Now, if my application itself currently holds N+1 strong references to objects which it's previously added…
Brian Harris
  • 2,735
  • 3
  • 22
  • 34
1
vote
2 answers

Where Weak and Soft references are used in Java EE programming

I am Java EE developer but I don't know where in day to day programming one might use Weak or Soft references.
changed
  • 2,103
  • 8
  • 36
  • 56
1
vote
3 answers

Java SoftReference, panicing GC and GC behavior

I want to write a cache using SoftReferences using as much memory as possible, as long as it doesn't get too inefficient. Trying to estimate the used size by calculating object sizes or by getting some used memory approximation of the JVM are dead…
Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
1
vote
1 answer

google-guava MapMaker .softValues() - values don't get GC-ed, OOME: HeapSpace follows

I am having trouble using the MapMaker from google-guava. Here is the code: package test; import java.lang.ref.SoftReference; import java.util.Map; import java.util.Random; import com.google.common.collect.MapEvictionListener; import…
wujek
  • 13
  • 2
1
vote
1 answer

A rare usage of WeakReference?

I have a class whose instances are initialized and used by underlying flatform. class MyAttributeConverter implements AttributeConverter { public YY convertToDatabaseColumn(XX attribute) { return null; } public XX…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1
vote
2 answers

Mixing different reference types in one collection

SoftReference, WeakReference, PhantomReference may be used to customize the process of garbage collection. All of them extend Reference therefore it is possible to mix them in single collection. Hard references (most common ones) do no extend…
1
vote
1 answer

Java SoftReference priority

I have an app that has several very large blocks of data which take a long time to compute. I hold each of these as a SoftReference because I can recompute them but it takes a long time. So as long I have enough memory I want to reuse what I have…
Brian McCormick
  • 449
  • 4
  • 5
1
vote
1 answer

Rationale for Soft-/Weak-/PhantomReferences clearing references to objects which have reference to tracked object

The documentation for Soft-, Weak- and PhantomReferences all include a line simiar to the following (taken from PhantomReference): At that time it will atomically clear all phantom references to that object and all phantom references to any other…
Marcono1234
  • 5,856
  • 1
  • 25
  • 43
1
vote
3 answers

How can I force release soft reference

I know soft reference will only release when the JVM is running low on memory. How can I do it manually? My code: Object object = new Object(); ReferenceQueue queue = new ReferenceQueue(); SoftReference softReference= new…
Idan Str
  • 614
  • 1
  • 11
  • 33