softreference

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 called a "phantom reference". That's 3 levels right there which the GC has a different be...

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 reliably get the soft references to be cleared. Simply using up a bunch of memory doesn't do...

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. ...

Is there any way to determine if an object in Java is softly reachable?

In order to perform some testing, I'd like to check how my application behaves when some or all of the objects I have stored in a cache of SoftReference'd objects are disposed of. In order to do this, I'd like to manually clear the references stored in the cached SoftReference objects - simulating the VM disposing of those objects - but...

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 is no way to enforce limits of this cache nor plan how much memory it can use. So I have in...

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 they called soft-references in they they are soft and break when "put under stress" ie:the...

Java: any problems/negative sides of keeping SoftReference to ArrayList in HttpSession?

My code is doing the following (just as an example, and the reason that I specify package path to java.lang.ref.SoftReference is to note that it's not my own implementaiton :-): ... List<String> someData = new ArrayList<String>(); someData.add("Value1"); someData.add("Value2"); ... java.lang.ref.SoftReference softRef = new SoftReference...

Testing code that uses SoftReference<T>

To get any code with SoftReference<T> to be fully tested, one must come up with some way to test the 'yup, it's been nulled' case. One might more or less mock this by using a 'for-test' code path to force the reference to be null, but that won't manage the queue exactly as the GC does. I wonder if anyone out can share experience in setti...

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 references as strong, WeakReference as weak and SoftReference as something in between (may be...

How to make the java system release Soft References?

Hi guys, 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 server again after the memory cleanup occurs. Here I find the problem how to mak...

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 when the compressor task gets to the object, if it is already gone we needn't bother compre...

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 after I see "GC freed bla-bla-bla bytes" in LogCat. Also, I saw a comment by Mark Murphy...