Clearing ColdFusion memory using garbage collection when memory gets low

I have been paining over this for ages but after finally trawling the web I finally found a post which was soooo simple but very effective in clearing the consumed memory. Link to post

What I did do was take it a stage further by building it into my Application.cfc so upon each request I would check for free memory and run the garbage collection accordingly if required.

In the OnRequestStart section I added the following code (Remove the CFFILE part as that's only there so I can see when ColdFusion does clear the garbage - Plus you can alter the free memory limit depending on your personal tastes):-

    <cffunction name="onRequestStart" access="remote">
    <cfargument name="targetPage" type="String" required=true/>
        <cftry>
            <cflock name="checkMemory" type="exclusive" timeout="1" throwontimeout="yes">
                <cfset runtime = CreateObject("java","java.lang.Runtime").getRuntime()>
                <cfset freeMemory = runtime.freeMemory() / 1024 / 1024>
                <cfif freeMemory lt 100>
                    <cfset objSystem = CreateObject( "java", "java.lang.System" )>
                    <cfset objSystem.gc()>
                    <cffile action="write" output="Garbage collected at #now()#" file="c:\garbage_collection.htm">
                </cfif>
            </cflock>
            <cfcatch type="any">
                <cffile action="write" output="Garbage collection failed at #now()#" file="c:\garbage_collection_failed.htm">
            </cfcatch>
        </cftry>

      ... YOUR CODE GOES HERE ...
 
        <cfreturn true>
    </cffunction>

Now, I haven't yet evaluated any performance penalties on a live server but I'm prepared for a small hit if it stops the server running out of memory then I'm happy.

 

InvalidTag encountered in web pages using ColdFusion due to cross site scripting protection

As you can see, I'm using Raymond Camdens excellent blog here. I recently tried to create a pod with some Javascript in it but every time I save the content the <SCRIPT...> tags gets converted to <INVALIDTAG...> - After some Googling and looking at a blog entry in Ramonds site it got me thinking about a workaround.

The problem lies with ColdFusion's cross site scripting tool. If for any reason you can't disable it or don't want to for security reasons the the code attached can sort this out for you.

There are 2 files that need creating - One in the client folder and one in the admin folder. They run at the end of the request and use the GetPageContext() function to replace INVALIDTAG which ColdFusion has already replaced and convert it back to a SCRIPT tag.

onRequestEnd.cfm to go into the blog /client/ folder

<!--- Kludge to get around ColdFusion cross site scripting replacement of SCRIPT tags to INVALIDTAG

Martin Parry - 7th June 2008

martin.parry@beetrootstreet.com

--->

 

<cfset pageContent = getPageContext().getOut().getString()> <cfset getPageContext().getOut().clearBuffer()> <cfset pageContent = ReplaceNoCase(pageContent , "<invalidtag", "<script", "all")> <cfoutput>#pagecontent#</cfoutput> <cfabort>

onRequestEnd.cfm to go into the blog /client/admin/ folder

<cfinclude template="../onRequestEnd.cfm">
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5.002. Contact Blog Owner