ColdFusion Performance

From Hostek.com Wiki
Revision as of 14:23, 24 January 2014 by Jakeh (Talk | contribs) (Garbage Collection Performance)

Jump to: navigation, search

When analyzing ColdFusion's performance, There are several things to keep in mind including JVM (memory) settings, slowness on the database tier, and memory leaks that can be easily avoided.

JVM Settings

Sizing the Heap

A good set of 64-bit JVM arguments to start with (for controlling memory and garbage collection) are below:
java.args=-server -XX:+UseCompressedOops -XX:+DisableExplicitGC -Xms1024m -Xmx1024m -XX:NewRatio=4 -XX:SurvivorRatio=8 -XX:PermSize=256m -XX:PermSize=256m -Xss256k -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:+CMSClassUnloadingEnabled -XX:+CMSScavengeBeforeRemark -XX:CMSInitiatingOccupancyFraction=65 -XX:+UseParNewGC

Those settings specify a maximum ColdFusion heap of 1 GB (1024 MB), an Eden space 1/4 the size of the heap (256 MB), and a non-heap Perm space of 256 MB. If your application needs a bit more memory, you can adjust the "-Xmx" and "-Xms" values as needed. A good rule of thumb though is to set them to the same value as each other to help improve ColdFusion perform better. More info on tuning the JVM can be found in this article, but the recommended JVM settings above are great for most ColdFusion applications: Java Garbage Collection Tuning

Garbage Collection Performance

Because a lot of the JVM's processing time can be spent in garbage collection of memory, it can be helpful to enable Garbage Collection Logging to your server. In order to do so, just add the following arguments to your ColdFusion server's jvm.config file:
-Xloggc:gc-1.log -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:+PrintGCTimeStamps

If you wish to change the name of the log file, just change 'gc-1.log' to the filename you wish. By default, this file will be created in the same folder as your jvm.config file.

NOTE: This section primarily applies to VPS/Dedicated customers only. Our administrators make sure the Heap is properly sized on our shared ColdFusion servers.

Tuning the Database Tier

Add Indexes

Make sure all database fields are indexed properly, adding indexes for any columns used in a WHERE clause.

Tune Your Queries

Be sure to remove any 'SELECT *' statements in the code. Instead, explicitly name each column used in a query. Also, make use of query advisors such as the following:

Cache Queries (cfquery / cfstoredproc) Where Possible

Does your application have queries that could be cached? If so, enable caching on those queries as shown here: Good Developers Practice Safe Query Caching

The main takeaway is that your application should use the cachedwithin attribute on as many cfquery and cfstoredproc tags as possible. Query caching helps reduce the load on the database server and improves page response time noticeably.

To enable this attribute on your queries, just add the cachedwithin attribute to your cfquery or cfstoredproc tags. This example will cache a query for 1 hour 15 minutes:
<cfquery name="myquery" datasource="mydsn" cachedwithin="#createTimespan(0,1,15,0)#">

Preventing Known Memory 'Leaks'

Specify CFFunction Output Attribute

For any <cffunction> and <cfcomponent> tags, make sure that you have output="false" specified. If for some reason you do need output from the function, be sure to explicitly add the output="true" attribute. There is a bug in CF that leaks memory when the output function is left of function and component declarations.

Use Var Scope for Variables within CFFunction

Make sure all functions have their variables in the "var" scope, including queries. The varscoper utility here will check your code for any missing var statements in your code: http://varscoper.riaforge.org/ We highly recommend using this on a regular basis for all your sites.

Handle Bots and Spiders Properly

Because bots and spiders can create a lot of useless sessions when indexing sites, we recommend setting a custom session timeout as shown here: Lower Session Timeouts for Bots and Spiders

Debugging Individual Scripts

During non-peak hours for your site, try enabling the ColdFusion debug output and adding your IP to the remote debug IP list. Then load the most frequently accessed pages and grab the debug output. Once you have this, focus on improving the slowest sections of code first.

Additionally, if you have Seefusion on your VPS, it will hightlight the server's slowest scripts. You can also obtain stack traces of running scripts to find the root cause of any slowness.

NOTE: This section primarily applies to VPS/Dedicated customers only. We do not allow debugging on our shared ColdFusion servers, as it negatively impacts server performance. Also, Seefusion is not available to our shared hosting customers.