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
0
votes
2 answers

Android OutOfMemoryException on createScaledBitmap and the role of SoftReferences?

I got an OOM. I know it has been covered alot by previous questions but mine has to do with the internals of Android and Java in general. As I am loading images at random points I get this dreaded OOM exception. I do have my images in an HashMap>.…
dnkoutso
  • 6,041
  • 4
  • 37
  • 58
0
votes
1 answer

collection.immutable.Map[ K, SoftReference[ V ]] over google's MapMaker?

Assume a potentially multi-threaded environment. I want to use a map along with (value) caching. Why would I prefer one of collection.immutable.Map.empty[ K, SoftReference[ V ]] new com.google.common.collect.MapMaker.softValues.makeMap[ K, V ] over…
0__
  • 66,707
  • 21
  • 171
  • 266
0
votes
1 answer

HashMap implementation with the soft referenced values

I wanted to have a Map with key mapping to quite a big object. Since the map is going to be used as a cache, I wanted to make the values/entries referenced via soft links (java.lang.ref.SoftReference) to clear it on pure memory. But in this case, I…
androberz
  • 744
  • 10
  • 25
0
votes
0 answers

How CMS decides when to collect SoftReferences?

I wrote a memory sensitive cache using soft references. At first it worked well, but after a few days, memory pressure went high, then suddenly dropped. Well, this is expected, I want to the cache to hold data as long as possible but not so long to…
ntysdd
  • 1,206
  • 2
  • 9
  • 19
0
votes
1 answer

any way to tell if a method ended or local in method in no longer in use?

I need some solution that will help me tell for a some methods, in a case like this: void myMethod(...) { MyObject obj = new MyObject(); // do stuff } if this method has ended or if obj is unreachable (which will indicate the method has…
user967710
  • 1,815
  • 3
  • 32
  • 58
0
votes
1 answer

Is ReferenceQueue suitable for object pooling?

I'd like to use Buffer pooling in my library and thought about using SoftReferences to achieve an implicit return of objects and pool size balancing. So, by "suitable" I mean: Are they quite performant compared to explicit ArrayBlockingQueue, for…
Pavlus
  • 1,651
  • 1
  • 13
  • 24
0
votes
2 answers

Is This correct way to use Soft References

I created a cache using Soft References a while ago, but in trying to resolve a bug I'm getting concerned that actually I've done it incorrectly and it's removing objects when it shouldn't. This is how I've done it: private static final Map
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
0
votes
2 answers

OOM error and weird SoftReferences

I have application running on HotSpot jvm 1.8.0_45 with well packed 8GB heap. Application tries to allocate memory for new objects fails with heap space OOM error. I have looked at heap dump and found that most part of space are occupied by…
0
votes
1 answer

Android: soft reference to activity

InputMethodManager is keeping a soft reference to a destroyed activity. The below is in my HPROF dump histogram. android.view.inputmethod.InputMethodManager$ControlledInputConnectionWrapper @ 0x43b7f768 Native Stack mInputConnection…
Frank
  • 12,010
  • 8
  • 61
  • 78
0
votes
0 answers

Cache that has access to all existing items

I have a system where objects (for the purposes of this question they are immutable) are created based on a request object (could be as simple as a url or a long). They are created with a factory method and not with new. If an object for a request…
frozenkoi
  • 3,228
  • 22
  • 33
0
votes
3 answers

Map Size incorrect in Java. If I wrap Key and Value in WeakReference and then add into HashMap, printed size is different than expected

If I don't comment line 1 and comment line 2, line 1 causes OutOfMemoryError. If I do the reverse, it does not causes OutOfMemoryError because are wrapped in WeakReference. But i can't understand the output of line 3 ---- i : 3869 Size…
0
votes
1 answer

How to use Soft/WeakReference classes in a Android App?

I'm maintaining a big app with a huge number of images. My main problem is the app crashes when I use it because it produces out memory error. I'm trying to use SoftReferences and WeakReferences, I've read about it, but I don't know if I have to use…
beni
  • 3,019
  • 5
  • 35
  • 55
-4
votes
1 answer

SoftReference is not getting cleared by Java GC

I was trying to understand SoftReferences in Java which basically ensures clearing memories of SoftReferenced objects before throwing StackOverflowError. public class Temp { public static void main(String args[]) { Temp temp2 = new…
Pratik
  • 908
  • 2
  • 11
  • 34
1 2 3 4 5
6