ColdFusion Memory Usage Stats

March 15, 2004

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>

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

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)>
    % of Free Allocated Memory: #percentFreeAllocated#%<br>
    % of Available Memory Allocated: #percentAllocated#%<br>

Related Entries

3 people found this page useful, what do you think?


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!
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?
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
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!!
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 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.
You use mb. So that is megabits instead of megabytes?
After running a batch process,the max memory used is 372 MB.We tried to delete the session variables using StructDelete().Even after deleting the session variables the memory usage is still 372 MB .Is there any way to reduce or free up the used memory ?
Hello dudes....How can i see the reply for the query i have posted???
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.

Recent Entries


did you hack my cf?