Soft References In ColdFusion

<!---
	Check to see if our test refernce object has been
	created. If not, we will need to create and cached it.
--->
<cfif NOT StructKeyExists( APPLICATION, "Reference" )>
 
	<!--- Let's create a simple struct. --->
	<cfset objGirl = {
		FirstName = "Winona",
		LastName = "Ryder"
		} />
 
 
	<!---
		Now, let's create a soft reference to that object
		and cache that in the APPLICATION scope. No other
		references to it are going to exist, so there will
		be no hard references that force it to remain in
		memory.
	--->
	<cfset APPLICATION.Reference = CreateObject(
		"java",
		"java.lang.ref.SoftReference"
		).Init(
			objGirl
			)
		/>
 
 
	<!--- Log the creation. --->
	<cfset ArrayAppend(
		APPLICATION.Log,
		"#TimeFormat( Now(), 'hh:mm:ss' )# - Reference Created."
		) />
 
</cfif>
 
 
 
<!---
	At this point, we have our soft reference in the APPLICATION
	scope, but there is not saying whether or not the object
	will still be there. We need to get the value and see if it
	returns null. I am scoping the return value so that we can
	see if the key exists after our Get().
--->
<cfset REQUEST.Girl = APPLICATION.Reference.Get() />
 
<!---
	Check to see if Girl exists. If it does, then we got the
	object. If it does not, then the object in question has been
	cleaned up.
--->
<cfif StructKeyExists( REQUEST, "Girl" )>
 
	<!--- It exists. Log message. --->
	<cfset ArrayAppend(
		APPLICATION.Log,
		(
			"#TimeFormat( Now(), 'hh:mm:ss' )# - Exists - " &
			REQUEST.GirL.FirstName
		)) />
 
<cfelse>
 
	<!--- It no longer exists. Log message. --->
	<cfset ArrayAppend(
		APPLICATION.Log,
		"#TimeFormat( Now(), 'hh:mm:ss' )# - Does NOT Exist."
		) />
 
</cfif>
 
 
 
<!--- Check to see if we want to run the garbage collection. --->
<cfif (RandRange( 1, 5 ) EQ 3)>
 
	<!--- Get a reference to the runtime. --->
	<cfset objRuntime = CreateObject(
		"java",
		"java.lang.Runtime"
		).GetRuntime()
		/>
 
	<!--- Call garbage collection. --->
	<cfset objRuntime.GC() />
 
	<!--- Log message. --->
	<cfset ArrayAppend(
		APPLICATION.Log,
		"#TimeFormat( Now(), 'hh:mm:ss' )# - Garbage Collection."
		) />
 
</cfif>
 
 
 
<cfoutput>
 
	<h3>
		Application Log
	</h3>
 
	<!--- Output application log entries. --->
	<cfloop
		index="strMessage"
		array="#APPLICATION.Log#">
 
		#strMessage#<br />
 
	</cfloop>
 
</cfoutput>

For Cut-and-Paste