What is a use case for a soft reference in Java?
Asked Answered
M

4

15

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 they called soft-references in they they are soft and break when "put under stress" ie:the JVM has run out of memory. I understand weak references and phantom references but not really when these would be needed.

Maegan answered 10/3, 2010 at 23:46 Comment(0)
P
20

One use is for caching. Imagine you want to maintain an in-memory cache of large objects but you don't want that cache to consume memory that could be used for other purposes (for the cache can always be rebuilt). By maintaining a cache of soft-references to the objects, the referenced objects can be freed by the JVM and the memory they occupied reused for other purposes. The cache would merely need to clear out broken soft-references when it encounters them.

Another use may be for maintaining application images on a memory-constrained device, such as a mobile phone. As the user opens applications, the previous application images could be maintained as soft-references so that they can be cleared out if the memory is needed for something else but will still be there if there is not demand for memory. This will allow the user to return to the application more quickly if there is no pressure on memory and allow the previous application's memory to be reclaimed if it is needed for something else.

Pyriphlegethon answered 10/3, 2010 at 23:49 Comment(1)
just to add, it appears that soft references are in principle the same as the memory management system of Linux (or any other os) where pages are loaded on-demand. Just that Linux can implement several page replacement algorithms. What about Java Garbage Collector? Does it (or can it be configured to) use patterns of usage of objects to select the Soft Reference to Garbage Collect?Reeve
A
16

This article gave me a good understanding of each of them (weak, soft and phantom references). Here's a summarized cite:

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.

A soft reference is exactly like 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 be discarded at the next garbage collection cycle, but an object which is softly reachable will generally stick around for a while.

A phantom reference is quite different than either SoftReference or WeakReference. Its grip on its object is so tenuous that you can't even retrieve the object -- its get() method always returns null. The only use for such a reference is keeping track of when it gets enqueued into a ReferenceQueue, as at that point you know the object to which it pointed is dead.

Antecede answered 10/3, 2010 at 23:56 Comment(4)
+1 for the summary :) That's the kind of text I'm looking for :P As for the phantom references... it looks very strange, I guess its main purpose is to serve as an artifact for debugging tools or things like that.Gonzalez
@Oscar: Phantom references can also be used to create safer finalization than the standard finalize method (I read this in a profiling tool help text but found some discussion here - code-o-matic.blogspot.com/2009/01/…).Kristie
the link is 404 now ;(Bur
@xxy: thanks, I've replaced it by a link to the Internet archive now.Antecede
F
3

The best example I can think of is a cache. You might not mind dumping the oldest entries in the cache if memory became a problem. Caching large object graphs might make this likely as well.

Foreshank answered 10/3, 2010 at 23:48 Comment(0)
F
0

An example of how a SoftReference can be used as a cache can be found in this post.

Fronde answered 16/5, 2012 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.