Retrieving ColdFusion memory usage
How much memory is ColdFusion actually using? Well you could look at the jrun.exe process in task manager but it won’t really give you a clear picture and tell you the true tale. You might also install something like FusionReactor or use the ColdFusion Server monitor, but maybe you just want the numbers, don’t have CF enterprise or haven’t the money for FusionReactor (althought I’m sure it’s worth everyone minor monetary unit
).
To gain access to this information we’ll delve into the Java runtime CreateObject('java', 'java.lang.Runtime').getRuntime(). This will provide us with three magic numbers \o/:
.maxMemory()
This is the maximum amount of memory that can be used, once this is hit your server is going struggle more than myself trying to remember a shopping list.
.totalMemory()
Out of the maximum memory, this is how much we’re got reserved for usage. All of this won’t be used as the runtime always reserves a little more than it needs, just in case.
.freeMemory()
This is the amount of allocated memory (.totalMemory()) that isn’t being used right now. While a useful number to know, it’d probably be easier to work around the free memory in relation to the maximum allowed (.maxMemory()).
Example
With this information it’s easy enough to work out some percentages, total free memory and amount actually in use. Below is an example of this code along with displaying it in a table.
<cfscript>
jRuntime = CreateObject("java","java.lang.Runtime").getRuntime();
memory = StructNew();
memory.freeAllocated = jRuntime.freeMemory() / 1024^2;
memory.allocated = jRuntime.totalMemory() / 1024^2;
memory.max = jRuntime.maxMemory() / 1024^2;
memory.used = memory.allocated - memory.freeAllocated;
memory.freeTotal = memory.max - memory.allocated + memory.freeAllocated;
memory.percentFreeAllocated = (memory.freeAllocated / memory.allocated) * 100;
memory.percentAllocated = (memory.allocated / memory.max) * 100;
memory.percentUsedMax = (memory.used / memory.max) * 100;
memory.percentUsedAllo = (memory.used / memory.allocated) * 100;
memory.percentFreeTotal = (memory.freeTotal / memory.max) * 100;
</cfscript><html>
<head>
<style type="text/css">
html {font-size:62.5%; font-family:verdana;}
body {font-size:1.2em;}
table, input, select, textarea, button {font-size:1em;}
table {border-collapse:collapse;}
td, th {border:1px solid #aaa; padding:3px;}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th scope="col">Maximum</th>
<th scope="col" colspan="2">Allocated</th>
<th scope="col">Used</th>
<th scope="col">% Used (Maximum)</th>
<th scope="col">% Used (Allocated)</th>
<th scope="col" colspan="2">Free (Maximum)</th>
<th scope="col" colspan="2">Free (Allocated)</th>
</tr>
</thead>
<tbody>
<tr><cfoutput>
<td>#NumberFormat(memory.max, '_.999')# MB</td>
<td>#NumberFormat(memory.allocated, '_.999')# MB</td>
<td>#NumberFormat(memory.percentAllocated, '_.99')# %</td>
<td>#NumberFormat(memory.used, '_.999')# MB</td>
<td>#NumberFormat(memory.percentUsedMax, '_.99')# %</td>
<td>#NumberFormat(memory.percentUsedAllo, '_.99')# %</td>
<td>#NumberFormat(memory.freeTotal, '_.999')# MB</td>
<td>#NumberFormat(memory.percentFreeTotal, '_.99')# %</td>
<td>#NumberFormat(memory.freeAllocated, '_.999')# MB</td>
<td>#NumberFormat(memory.percentFreeAllocated, '_.99')# %</td>
</tr></cfoutput>
</tbody>
</table>
</body>
</html>
CF Code Highlighter in ColdFusion
Ever wanted to highlight some ColdFusion code? Well since at least CF7, ColdFusion can do the job for you. Hidden away is a Java object that you can use to format source code directly from a file or from a string. Below is the code used to do this, along with a screen shot of the CF highlighted output.
<html>
<body>
<cfscript>
// Create the object
jFormat = CreateObject('java', 'coldfusion.util.FormatSource');
fromFile = jFormat.formatFile(GetCurrentTemplatePath());
fromString = jFormat.formatString('
<!--- Comment --->
<cfset this = "that" />
');
</cfscript>
<h2>From File</h2>
<pre><cfoutput>#fromFile#</cfoutput></pre>
<hr />
<h2>From string</h2>
<pre><cfoutput>#fromString#</cfoutput></pre>
<!--- Bunch of code to highlight... --->
<cfsilent>
<cfset qTest = QueryNew('id,label,amount') />
<cfloop from="1" to="100" index="i">
<cfset QueryAddRow(qTest, 1) />
<cfset QuerySetCell(qTest, 'id', i, i) />
<cfset label = '' />
<cfset length = RandRange(3, 10) />
<cfloop from="1" to="#length#" index="c">
<cfset label = label & Chr(RandRange(65, 90)) />
</cfloop>
<cfset QuerySetCell(qTest, 'label', label, i) />
<cfset QuerySetCell(qTest, 'amount', RandRange(100, 999), i) />
</cfloop>
<cfquery name="qTest" dbtype="query">
SELECT * FROM qTest WHERE amount > 50000
</cfquery>
</cfsilent>
<!--- End of useless code to highlight --->
</body>
</html>
Cardiff City Football statue poll
The Cardiff City Supporters Trust is having a public vote, on 3 designs, for a statue honouring their legendary captian Fred Keenor. If you don’t know who that is and why he’s important to Cardiff City Football Club you should read up on him, as he captained the Cardiff squad which won the FA Cup and took it out of England for the only time in it’s history (so far
).
I’d like to suggest Michael Fields design (Number 2), it’s an outstanding piece of work and well deserving of my vote (and yours if you like). With the new stadium and the nearby shops, I’m in the area quite often and out of all of the designs I’m pretty sure that’s the one which would look right at home outside it.
Please place your votes via the Cardiff City Supporters Trust website (bottom of the second column) or using forms in the South Wales Echo. Voting closes on Saturday the 12th of December.
Evaluating Implicit Struct and Array Creation
Since ColdFusion 8 (8.0.1 for nested awesomeness) and Railo some-version-or-other, we’ve been able to create arrays and structures with the following shorthand syntax.
<cfset st = {mister = 'dai'} />
<cfset ar = [1,2,3,4, 'mister', 'dai'] />
<cfset star = {mister = ['dai', 'dave']} />
This is great and should be old hat for a lot of developers who have worked in CF8+. Annoyingly though, you can’t evaluate it. Why would anyone want to? Because I just do okay! Maybe I’d like to store a data structure in the database and simple evaluate it from there. I’ve tried several approaches, shown below, and for Adobe ColdFusion 8/9 none of them worked. Railo however came to the rescue and worked really well, giving me exactly what I needed!
Code used for testing Evaluate
<dl>
<dt><cfset cfon = '{misterdai= "dave"}' /><br />
<cfset Evaluate(cfon) />
</dt>
<dd>
<cftry>
<cfset cfon = '{misterdai= "dave"}' />
<cfset evaluate(cfon) />
Yes
<cfcatch type="any">
No
</cfcatch>
</cftry>
</dd>
<dt><cfset cfon = "{misterdai= 'dave'}" /><br />
<cfset Evaluate(cfon) />
</dt>
<dd>
<cftry>
<cfset cfon = "{misterdai= 'dave'}" />
<cfset evaluate(cfon) />
Yes
<cfcatch type="any">
No
</cfcatch>
</cftry>
</dd>
<dt><cfset cfon = '{misterdai: "dave"}' /><br />
<cfset Evaluate(cfon) />
</dt>
<dd>
<cftry>
<cfset cfon = '{misterdai: "dave"}' />
<cfset evaluate(cfon) />
Yes
<cfcatch type="any">
No
</cfcatch>
</cftry>
</dd>
<dt><cfset cfon = "{misterdai: 'dave'}" /><br />
<cfset Evaluate(cfon) />
</dt>
<dd>
<cftry>
<cfset cfon = "{misterdai: 'dave'}" />
<cfset evaluate(cfon) />
Yes
<cfcatch type="any">
No
</cfcatch>
</cftry>
</dd>
<dt><cfset cfon = "<cfset output = {misterdai: 6} />" /><br />
<cfset Evaluate(cfon) />
</dt>
<dd>
<cftry>
<cfset cfon = "<cfset output = {misterdai: 6} />" />
<cfset evaluate(cfon) />
Yes
<cfcatch type="any">
No
</cfcatch>
</cftry>
</dd>
<dt><cfset cfon = "<cfset output = {misterdai= 6} />" /><br />
<cfset Evaluate(cfon) />
</dt>
<dd>
<cftry>
<cfset cfon = "<cfset output = {misterdai= 6} />" />
<cfset evaluate(cfon) />
Yes
<cfcatch type="any">
No
</cfcatch>
</cftry>
</dd>
<dt><cfset cfon = "<cfscript>output = "{misterdai: 6}"; </cfscript><br />
<cfset Evaluate(cfon) />
</dt>
<dd>
<cftry>
<cfset cfon = "<cfscript>output = {misterdai= 6};</cfscript>" />
<cfset evaluate(cfon) />
Yes
<cfcatch type="any">
No
</cfcatch>
</cftry>
</dd>
<dt><cfset cfon = "<cfscript>output = "{misterdai= 6}"; </cfscript><br />
<cfset Evaluate(cfon) />
</dt>
<dd>
<cftry>
<cfset cfon = "<cfscript>output = {misterdai= 6};</cfscript>" />
<cfset evaluate(cfon) />
Yes
<cfcatch type="any">
No
</cfcatch>
</cftry>
</dd>
</dl>
Results
| Code | Adobe CF 8.0.2 |
Adobe CF 9 |
Railo 3.1.1 |
|---|---|---|---|
<cfset cfon = '{misterdai= "dave"}' />
<cfset evaluate(cfon) />
|
No | No | Yes |
<cfset cfon = "{misterdai= 'dave'}" />
<cfset evaluate(cfon) />
|
No | No | Yes |
<cfset cfon = '{misterdai: "dave"}' />
<cfset evaluate(cfon) />
|
No | No | Yes |
<cfset cfon = "{misterdai: 'dave'}" />
<cfset evaluate(cfon) />
|
No | No | Yes |
<cfset cfon = "<cfset output = {misterdai: 6} />" />
<cfset evaluate(cfon) />
|
No | No | No |
<cfset cfon = "<cfset output = {misterdai= 6} />" />
<cfset evaluate(cfon) />
|
No | No | No |
<cfset cfon = "<cfscript>output = {misterdai= 6};</cfscript>" />
<cfset evaluate(cfon) />
|
No | No | No |
<cfset cfon = "<cfscript>output = {misterdai= 6};</cfscript>" />
<cfset evaluate(cfon) />
|
No | No | No |
Conclusion
Unfortunately for me, I’m kind of stuck with using Adobe ColdFusion for the application I’m working on. My only workaround was to use JSON and the “deserializeJson” function. This works just as well for my usage but I’d still like evaluate to work eventually.
Zend Server + WinCache + Windows
After reading a benchmark post comparing WinCache against Zend Optimizer, on MS Web Platform and Zend Server respectively, I thought I’d try Zend Server (CE) with WinCache. The reasoning behind this is a bug with Zend Optimizer that I’m suffering from (using PHP 5.2) where it’ll randomly crash the php-cgi.exe process. This only appears to be a problem with certain code contained somewhere within Moodle that it doesn’t like.
Setup
- Zend Server Community Edition 4.0.5
- Windows Server 2003
- WinCache 1.0 RC
- Moodle
Benchmarks
It’s only a small dev server so doesn’t expect massive numbers
Zend Server CE (No accelerators)
Concurrency Level: 5 Time taken for tests: 139.669 seconds Complete requests: 500 Failed requests: 0 Write errors: 0 Total transferred: 16600000 bytes HTML transferred: 16259500 bytes Requests per second: 3.58 [#/sec] (mean) Time per request: 1396.685 [ms] (mean) Time per request: 279.337 [ms] (mean, across all concurrent requests) Transfer rate: 116.07 [Kbytes/sec] received
Zend Server CE 4.0.5 + WinCache 1.0 RC
Concurrency Level: 5 Time taken for tests: 45.861 seconds Complete requests: 500 Failed requests: 0 Write errors: 0 Total transferred: 16600000 bytes HTML transferred: 16259500 bytes Requests per second: 10.90 [#/sec] (mean) Time per request: 458.615 [ms] (mean) Time per request: 91.723 [ms] (mean, across all concurrent requests) Transfer rate: 353.48 [Kbytes/sec] received
Zend Server CE 4.0.5 + Zend Optimizer
Concurrency Level: 5 Time taken for tests: 36.207 seconds Complete requests: 500 Failed requests: 0 Write errors: 0 Total transferred: 16600000 bytes HTML transferred: 16259500 bytes Requests per second: 13.81 [#/sec] (mean) Time per request: 362.072 [ms] (mean) Time per request: 72.414 [ms] (mean, across all concurrent requests) Transfer rate: 447.73 [Kbytes/sec] received
Summary
- ZSCE = 3.58 requests / second
- ZSCE + WinCache = 10.90 requests / second
- ZSCE + Zend Optimizer = 13.81 requests / second
Conclusion
While WinCache appears to be a good solution, Zend Optimizer appears to have a little lead on it. Comparing it to the original benchmark I mention at the top of this post, I’d say a lot of this is dependant on the acutal code used. Personally, I’m going to stick with WinCache. It has the advantage of being open source, it works really well with my fastCGI server of ZSCE and most importantly it doesn’t suffer from a bug that Zend Optimizer does where it doesn’t like something about the Moodle code and will randomly crash.
Long term, I’d probably go with Zend Optimizer as I like the idea of having a complete server package of ZSCE where I don’t have to customise it too much.


Recent Comments