Skip to main content

Add the ability to GC soft references

4 replies [Last post]
Joined: 2005-06-30

Please add the ability to collect all soft references, e.g., Runtime.cleanSoftReferences().

I am using caches, which implemented using soft references (java.lang.ref.SoftReference). All objects are in the cache, but the dirty ones are also hard-referenced. The SoftReference interface assures that they will not be GCd, and I can safely find them using the soft referenced cache.

In order to check code correctness, I want to simulate low-memory conditions, dropping all references to objects, which are not hard referenced as well. Adding such a method will help a lot.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-10

You might find this interesting:

It provides a brief overview of the 1.3.1+ SoftRefLRUPolicyMSPerMB flag.

Joined: 2003-06-15

If you use -Xmx and a large array you'll achieve the low memory condition that you're looking for.

Joined: 2004-09-02

I can't see immediately how about this is necessary; try -Xmx option ;)

Joined: 2003-06-19

A bit of reflection to modify the timestamp field and System.gc should do the same job, I think. Or substitute WeakReferences for the SoftReferences.