At cf.Objective(), Mark Mandel held a presentation on the caching mechanisms and algorithms he uses in his Transfer ORM project. In it, he talked about his use of Soft References. A soft reference, as opposed to a hard reference, is a reference to an object that does not get flagged as a "use" of that object. When the JVM cleans up all the garbage in its memory, it checks to see if objects are in use by seeing if there are any hard references to it. If there are no hard references, the garbarge collection may clear that object out of memory. From what I gathered, Mark uses soft references to keep cached items around until the JVM needs to free up memory.
To do this, he uses the java.lang.ref.SoftReference class. I had never heard of this, and I love playing with the underlying Java, so I figured I would take a look and see how this object works. The SoftReference class is really simple. It basically takes an object in its constructor and then provides a Get() method to retreive that object (to which it has a soft reference). If the object has been cleared from memory, the Get() method will return NULL, and in ColdFusion, will delete whatever variable stored the return object.
To test this, I set up a really simple application that stores a single soft reference. I then would refresh the page repeatidly to see if the object in question still existed. To try and speed up the process, I put in some random Garbage Collection calls. I have been told that you can't really force garbage collection, but I threw it in anyway (I might not even be using it properly - I don't know that much about Java). Anyway, here is my very simple Application.cfc:
Launch code in new window » Download code as text file »
All we are really doing here is initializing the APPLICATION scope and creating an array of log messages.
Then we have our index.cfm page which creates the soft reference if it doesn't exist and then, for each subsequent page request, sees if our soft reference target still exists:
Launch code in new window » Download code as text file »
As you can see, for each page request, I am calling the Get() method on our cached SoftReference object. If that return value voids the "Girl" key in the REQUEST scope, we know that the soft reference target has been cleared by memory. I ran this a bunch of times and here it the log output that I got:
Application Log
07:57:11 - Reference Created.
08:03:28 - Exists - Winona
08:03:30 - Exists - Winona
08:04:36 - Exists - Winona
08:09:33 - Exists - Winona
08:09:33 - Garbage Collection.
08:09:34 - Exists - Winona
08:09:34 - Garbage Collection.
08:09:35 - Exists - Winona
08:09:36 - Exists - Winona
08:09:44 - Exists - Winona
08:09:45 - Exists - Winona
08:11:27 - Exists - Winona
08:11:27 - Garbage Collection.
08:12:06 - Exists - Winona
08:12:08 - Exists - Winona
08:12:08 - Garbage Collection.
08:13:21 - Exists - Winona
08:15:28 - Exists - Winona
08:27:14 - Exists - Winona
08:27:21 - Garbage Collection.
08:27:22 - Exists - Winona
08:37:35 - Exists - Winona
As you can see, over the course of about 30 minutes, I refreshed the page ever few minutes. As easy as this was to set up, apparently, it's not very easy to get the soft reference target to be cleared out of memory. I guess the JVM just didn't see the need - I have almost nothing running on this DEV server at this time of the day. There are no memory management issues that need to taken into account.
Not much to take away from this post as I couldn't demonstrate the very idea that I set out to test. At the very least, the idea of soft references is very interesting. Thanks to Mark Mandel for bringing this to my attention.
Download Code Snippet ZIP File
Comments (7) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
A couple things - I have been able to force garbage collection using the jConsole tool. I have been planning on writing a blog post about it but haven't gotten to it yet.
The other things you could try to do to get that object to be cleaned up would be
1) lower your -Xmx argument in jvm.config so you are running with less memory
2) Run another CF page that repeatedly stuffs something large into a persistant scope, to start using up available memory.
It would be interesting to see it work.
Posted by Ryan Stille on May 6, 2008 at 9:32 AM
@Ryan,
I don't want to start messing with the system settings too much; I don't know very much about that stuff and I don't want to break anything. I have heard of people breaking the CF service and stuff by messing with the configuration. Eeek :)
Posted by Ben Nadel on May 6, 2008 at 9:41 AM
I just realized I totally forgot to Spell Check this blog post. So appologies to anyone who finds blatant spelling errors.
Posted by Ben Nadel on May 6, 2008 at 9:46 AM
mmh, pretty interesting post man!
cheers
Posted by sal on May 6, 2008 at 12:58 PM
Soft References are indeed great Ben. ColdBox's caching engine is based on soft references too. It also supports caching eternal references that won't get cleaned by the JVM.
As a side note, I don't recommend running garbage collections manually, that is just asking for trouble. What you can do to limit their stay in memory is to be able to track the time they where placed in the cache and have a reaping method that checks how long they have lived. This way you can create timeouts on the objects. Although they are not always guaranteeded, since the JVM can collect them whenever it sees fit.
Also, remember that if you have any references to these objects in such a way that the JVM sees them as hard references, they won't be collected.
Posted by Luis Majano on May 6, 2008 at 4:08 PM
@Luis,
Agreed. I never use garbage collection. I only did it here because I wanted to see that reference disappear and it just wouldn't!! My first attempt didn't have the GC at all. I only put it in after like 15 minutes when the reference did not disappear.
Posted by Ben Nadel on May 6, 2008 at 5:42 PM
Ben, I too found this fascinating. I want to use it at work. So I ran some tests on caching CFC instances. I used lots of them and watched the memory monitor in CF8. After GCs, I was able to find missing ones. Check out my experiment.
http://www.cfchris.com/cfchris/index.cfm/2008/5/7/javalangrefSoftReference-in-CFML
Posted by Chris Phillips on May 7, 2008 at 3:19 PM