Pete Freitag Pete Freitag

ColdFusion Memory Usage Stats

coldfusionjava

Here are some code to find out some info about the memory usage of CFMX or BlueDragon. The Java API provides three methods in the java.lang.Runtime class: freeMemory, totalMemory, and maxMemory. These methods provide information about the JVM's memory usage, and its easy to access them with CFML:

<cfset runtime = CreateObject("java","java.lang.Runtime").getRuntime()>
<cfset freeMemory = runtime.freeMemory() / 1024 / 1024>
<cfset totalMemory = runtime.totalMemory() / 1024 / 1024>
<cfset maxMemory = runtime.maxMemory() / 1024 / 1024>

<cfoutput>
    Free Allocated Memory: #Round(freeMemory)#mb<br>
    Total Memory Allocated: #Round(totalMemory)#mb<br>
    Max Memory Available to JVM: #Round(maxMemory)#mb<br>
</cfoutput>

From these numbers we can also determine the percent of free allocated memory available, and also the percent of avalaible memory allocated

<cfset percentFreeAllocated = Round((freeMemory / totalMemory) * 100)>
<cfset percentAllocated = Round((totalMemory / maxMemory ) * 100)>
<cfoutput>
    % of Free Allocated Memory: #percentFreeAllocated#%<br>
    % of Available Memory Allocated: #percentAllocated#%<br>
</cfoutput>

Like this? Follow me ↯

ColdFusion Memory Usage Stats was first published on March 15, 2004.

If you like reading about jvm, cf, java, heap, monitoring, or memory then you might also like:

FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.

Comments

Great info! I haven't seen this anywhere else, and it will be very helpful in diagnosing problems here in ColdFusion Support, when combined with CFLOG. My eyes are a bit weary of reading the verbose:gc logs. Thanks!
by Steven Erat on 03/17/2004 at 12:02:01 PM UTC
I notice when I repeatingly hit the refresh button real quickly I see the memory go up rather quickly until it gets to a point were it can no longer serve pages. I am running the Pro standalone version of MX. Is their a JRun setting I can make to stop this from happenning?
by James Ruzicka on 04/09/2004 at 2:40:32 PM UTC
Hey, great tip. Took some of this data and created a graph of it. Good stuff. Any ideas how we could get even more detailed information, such as the currently running threads or templates? Tony
by Tony on 04/13/2004 at 12:40:57 PM UTC
Its gr8 info but how to get these info in a situation where i cant even access my cfm template ? Ex: we are facng a problem with jrun 4 and cfm in win2k memeory iis keepon growing ,in this case we ant run any cfm template!!
by vishnu on 06/23/2004 at 8:23:42 AM UTC
Just a note: since the maxMemory method was added in Java 1.4, it may be necessary to test for the version before running that method. A suitable test (not the best) would be: CFIF left(system.getproperty("java.version"),3) ge 1.4 This is particularly useful on BlueDragon/.NET, since the underlying Java language support in the .NET framework is about at 1.1 with additional enhancements.
by Charlie Arehart on 12/30/2004 at 3:10:46 PM UTC
By god this chews up server resources... Hold down your F5 key for a second and it ties up your server big time.. I tried putting an exclusive server lock around it which helped (I could only press it repeatedly about 10 times and it hung before) - at least now you need to hold it down for a short time. If I was to use this in live code I'd have to restrict the refreshes from a client.. Good snippet though - thanks.
by Martin Parry on 01/14/2005 at 11:35:04 AM UTC
You use mb. So that is megabits instead of megabytes?
by Johnny on 05/08/2007 at 2:31:29 PM UTC
Prasanth, you're laboring under a couple of misunderstandings. First, deleting session variables in a given request may have no significant effect on your memory use. That would only remove the session variables for that one session. You could have dozens (or hundreds) of other sessions for other users. Beyond that, your memory could be used by many things, including other shared scope variables like application and server, or such things as cached queries, cached templates, and more. Only with CF8's server monitor or its API (both available in Enterprise only) are you able to get CF to tell you exactly what things are using what amount of memory. And even then, you can't expect that just because you delete variables that the memory used will be immediately available. As of CF 6+, it isn't C++ but Java that underlies CF, and so your deletion of variables only lets the Java garbage collector know that you're willing to have it reclaim the memory used. You have no control over when that will happen (even a forced GC is just a request to the JVM to give it a shot.) Finally, you ask "how can I see the reply for the query i have posted?", as if you expect that a question raised should be answered. I don't want to speak for Pete, but no blog is a tech support mechanism. The folks who offer their thoughts (including the blogger) are all offering them on a volunteer basis. Maybe this was just a language issue, but someone could interpret your note to be kind of demanding. Just sharing that for your future consideration.
by Charlie Arehart on 02/06/2008 at 10:50:24 AM UTC