I'm not going to get into the pros and cons of manually calling the garbage collector but feel free to hash it out in the comments.
Anyway, here is how you check your currently used Heap and, if you want, how you call the garbage collector.
<cffunction name="cleanGC">
<cfset var rt = createObject("java","java.lang.Runtime") />
<cfset var used = rt.getRunTime().totalMemory() - rt.getRuntime().freeMemory() />
<cfset var half = rt.getRunTime().totalMemory()/2/>
<cfif used GT (half)><!--- more than half as many megs as are available being used? Clean up if we can --->
<!--- call garbage collection --->
<cfset rt.getRunTime().gc() />
</cfif>
</cffunction>
It's pretty straight-forward really. Use at your own risk.
3 comments:
I saw a very minor issue in your code, you repeat the <cffunction line twice.
other than that nice post.
regards,
larry
Thanks Larry. I'll fix it as soon as im at a real computer
Thanks Larry. I'll fix it as soon as im at a real computer
Post a Comment