The amazing adventures of Doug Hughes

Archive for August, 2009

Setting the edit position on the Datagrid

Just quickly wanted to describe setting the edit position on the Flex Datagrid component.

I recently wanted to add a new empty row to my datagrid and make sure that new row had focus and was ready to be edited. Setting the editedItemPosition property on the datagrid requires passing an object in the following format:

{ rowIndex : 0 , columnIndex : 1 }

Here’s some code to get you there if your looking to do something similar:

protected var dataGridDataProvider: ArrayCollection = new ArrayCollection([
            name: 'firstName',
            description: 'First Name'
        }, {
            name: 'lastName',
            description: 'Last Name'
        }, {
            name: 'email',
            description: 'Email'
     * Handles the click event from the add row btn.
     * @param	event	MouseEvent
    public function addRowBtnClickHandler(event: MouseEvent): void {
        // add am empty item
        dataGridDataProvider.addItem({name:'', description:''});
        // select the empty item
        dataGrid.selectedIndex = dataGridDataProvider.length - 1;
        // set the new item to edit
        dataGrid.editedItemPosition = {rowIndex:dataGrid.selectedIndex, columnIndex:1}

/ * * * Handles DataGridEvent that is dispatched when an item editing session ends on the dataGrid. * * @param event DataGridEvent * /
public function dataGridItemEditEndHandler(event:DataGridEvent):void
trace('Edit the Datagrid.')

Hope this helps some folks and thanks for any comments.

Troubleshooting ColdFusion Performance: Analysis Part II

This is part 3 in my Troubleshooting Coldfusion Performance series. It was briefly interrupted by a great trip to CFUnited 2009, then catching up, but here it finally is! Several folks approached me at CFUnited with encouragement and enthusiasm for this series, I cannot tell you how much I appreciate that! I’m glad I can finally give something back to a community that has given me so much over the years!

As this is part 3, you will most likely want to take a quick read over the 1st 2 posts in this series.

So at this point, we have isolated and resolved a SQL performance issue using some of the tools that come with MS SQL. While this does not mean that our SQL configuration is now completely optimal, it has resolved the largest issue we are observing here, so we move on. Now its time to tackle our out of memory issues.

When your system is getting an Out of Memory exception, it can actually make it easier to debug than errors that only happen sporadically. There is a jvm argument that you can add to your jvm.config file that will cause a heap dump to be generated every time your server runs out of memory. Add this argument to your jvm.config file:


Once you have this argument added, restart ColdFusion. Then you have the fun part of waiting for (or causing) an out of memory condition to occur. For Project X, we have 4 servers in our cluster, so we will add this argument to 1 server and bring it back into the cluster. Once it runs out of memory, the server will take a few minutes to shut down after it crashes, as it will build a text file in your {coldfusion}/bin folder with the contents of your memory at the time that the server went crashed.

Now you have a file, but if you tried to open your file in notepad, you may cause yourself more issues (the files are most often hundreds of megs, if not gigs in size). There are several tools you can use now to open your heap dump and identify possible memory leaks. Here are 3 that I find myself using.

  • Netbeans– Neatbeans is a full java IDE with tons of plugins, and includes a memory profiler and tools to view and explore a heap dump.
  • VisualVM– VisualVM is a tool dedicated to profiling, monitoring, and analyzing java application performance.
  • Memory Analyzer (MAT)– MAT can be run standalone or as a plugin integrated to Eclipse. MAT has a slick feature that will automatically try to analyze a heap dump to find possible memory leaks. While it has been hit or miss for me with ColdFusion code thus far, it has some great reporting and can make the heap dump a bit less intimidating.

So, this being a hypothetical project and all, I wrote a quick test case to show a simple memory leak. You can download it here and check it out yourself to verify that these methods work for tracking down objects that are staying in heap too long. After one request of the index.cfm file in my sample, capture a heap dump from within VisualVM. Load it up, and switch to ‘classes’ view. On the bottom of the classes view, you will see a filter. Type in ‘cfl’ then click the green check-mark. All of the CFC’s you create that go into memory are prefaced by ‘cf’, followed by your filename, then some compiler hash looking string of letters and numbers. In this simple test case, we are looking for ‘leaky.cfc’, so filtering on ‘cfl’ will find them. After 1 request, we will see something like the following.

first Request

Notice that the cfleaky2ecfc482229893 object shows 101 instances in memory. The 1 instance is our ‘application’ scope variable, and the 100 instances are as a result of us running ‘doSomething()’ against it. When you run the index.cfm file another few times, then capture another heap dump, you see something like this:

fifth Request

So lets say we are observing this on projectX, where we have a class file that its instances count keeps growing like this on every request. This can obviously start chewing up memory, and even after 20 minutes this memory did not get reclaimed in my local tests. Now, in ProjectX, our codebase will obviously be much larger than the 3 files in my test, and isolating the code that creates this object would be much harder than looking at 10 lines of code. To help us isolate this, we will use another technique I blogged about here, using hprof with ‘sites’ to show objects that are in heap, and their stack trace associated to their creation. For those who have not used a stack trace, its basically a road map on how the object in question was created. Think of a request, where one method calls another, which in turn calls another, and so on. Each method request will be a line item in your stack trace, which will eventually show you a file from which your object was created. After you stop your ColdFusion instance, you will get a file that is named {coldFusion root}/bin/java.hprof.txt. On my test, after making 20 requests or so, the file generated was about 32 megs in size. When you open it up, you will see a ton of stack trace objects at the top of the file. Scroll ALL the way down till you get to a list of classes and their count of instances in memory, and you will see:

hprof Sites

I highlighted 2 items here, the right most one is the class name (the name of the compiled template called leaky.cfc), and the number to the left of it is the stack trace index. Copy that number 368230 into the clipboard, then search for it in this file (above the list we are looking at now). You will come to the stack trace that created this object, which looks like:

stack Trace

This technique can help you quickly locate the source of leaky objects. Sometimes you will have to alter the depth of the stack trace to properly locate your object (this was run with a depth of 6, so it shows only the most recent 6 method calls). In this very simple test case, changing line 5 of index.cfm will resolve the extra objects in memory.

Before: cfset request.leaky = application.leaky

After: cfset request.leaky = duplicate( application.leaky )

This simple example is just an illustration of how you can get yourself into trouble when pointing a reference to objects from a shared scope like application or server, into a local scope like request. When we make a link from the request scope into the application scope, it maintains a reference such that the request variable would never go away until the referencing application variable gets garbage collected (which would wait for the application timeout). By duplicating the object into the request scope instead, this ensures there is no reference to the application scope variable, which allows it to be cleaned up after the request completes.

This technique is great for identifying poor code practices, or errors in your objects. With ProjectX, this technique will let you find and squash any memory leaks that are present. Running this through several times until you no longer observe a constantly growing heap size will eliminate your memory leak, and let us proceed to other issues!

Next post I will talk about the iterative process of tuning your JVM configuration, using jmeter to simulate load while you observe some of your system metrics.

Remember, anyone who comments on any post in this series will be entered into the drawing for an Alagad backpack, which anyone who has one can attest they are totally sweet. Please feel free to contribute here, any comments are welcome to clarify anything I left hanging, and I will try and answer any questions that are posted!

Presentation Files for Performance Tuning Coldfusion: Before the JVM from CFUnited

For anyone who missed it, or wanted to get some of the program links or jvm.config settings, here you go!
Download it from here.

'ColdFusion and jQuery: Perfect Together' Presentation Files

As promised, I have posted the presentation files – including all the code – for my CFunited presentation.
You can get the files here.

Alagad Quickies

  I have a few quick things I think people might be interested in, but rather than writing a bunch of small blog entries, I figured I’d wrap them up into one shot.

  First off, Alagad has released an all-new website. This new website separates the developer-focused content from the corporate marketing information. This should probably make both audiences happier. I personally love the new design and hope you do too. We’ll be adding more content including project descriptions, client information, event information, press releases and more. All things in good time!

  This week five of the Alagad team members (Scott Stroz, Brian Kotek, Chris Peterson, Vicky Ryder and myself, Doug Hughes) are at the CFUnited conference. We’re what I’d call a roaming sponsor, which means we didn’t spring for a booth. However, we’re not skimping on our conference give away. If you’re at CFUnited and you find an Alagad employee you can get entered into a raffle to win one of ten Alagad bags stuffed with a Mac Mini, a Wii, an iPod Touch, or an Amazon gift card. All you need to do is answer a short survey!

  Speaking of conferences, I’ve decided that we’re going to be sponsoring the Adobe Max conference this year. I’m excited since we’ve never done that before. I’m not sure we’ll do the big give away, but I’ll try to do something exciting. (Any ideas?)

  Finally, we have nine of the very nice Alagad backpacks left over from our conference giveaways. So we’re planning on holding a series of Blog-based contests with the winner getting one of the bags. Chris Peterson is running the first one right now. Keep your eyes on the Alagad blog for more information.

Troubleshooting Coldfusion Performance: The Problem

In this post series, I would like to put forward a hypothetical situation involving poor ColdFusion application performance, the investigative steps to take to isolate the issues, and the remedial steps to perform in order to solve those issues. I would really like some feedback from readers as well here, to hypothesize on possible issues, possible resolutions, and supply other tools or methods which may identify or solve the issues we discuss. I hope that this post series will not only help you identify and deal with ColdFusion issues, but also help to identify database, network, or hardware issues as they may arise. Note: This hypothetical situation, while pulled from my experiences, is not a direct parallel to any of my previous customers, and is instead a combination of factors from several different projects. Lets call it Project X.

The Environment

Project X is setup to run across (4) Coldfusion 8 Enterprise edition servers, in a load balanced cluster behind a hardware load balancer. Sticky sessions are configured, so once a user makes a request to a given server, their subsequent requests should continue on the same server. Project X has a single MS SQL 2005 database on a 32 bit Windows 2003, which has 4 GB of ram. This server has (3) 15K 75 gig SCSI hard drives in RAID 5, upon which the operating system and the MS SQL binaries are installed. There is an iSCSI connected device which has (8) 10K 147 gig SCSI hard drives. The iSCSI device contains the MS SQL data and log files. Each Project X web server is a 32 bit Windows 2003 server with 4 GB of ram, and (3) 15K 75 gig SCSI hard drives in RAID 5. Each web server is running 2 instances of ColdFusion in a local cluster (using round robin to split requests between instances), and each ColdFusion instance is using the default JVM configuration that ships with ColdFusion 8. There is a shared folder on the MS SQL server which contains all shared page assets (files uploaded by the users, PDF documentation, and images). Each web server is running Apache, and has an alias pointing to the shared folder on the SQL box (using a UNC path).

All servers are connected to a 24 port gigabit switch, and is hosted on an OC3 line. Project X is a web based file sharing application which allows users to upload and share files of many types (images, pdf’s, office documents, and more). It makes use of Application.cfm to load site variables, and uses several CFC objects to encapsulate database queries and user information.

The Problem

For several years this configuration has worked fine for the customer, with stable servers and acceptable response times. Project X has recently run an ad campaign in the national media, which has increased their site traffic by a factor of 2. Since the campaign, users have been complaining of slow web response times, as well as error messages. Investigating the server logs also shows that the coldfusion instances have been crashing with out of memory errors.You are tasked with uncovering the issues that are causing the slow page rendering, and the out of memory server issues. In my next post I will share both my techniques I use to identify these issues, as well as a selection of idea’s in comment responses to this post. Thoughts?


Now to encourage participation here, anyone who contributes in comments to this chain of blog posts by commenting (with a relevant comment to this discussion) will be entered into a drawing to win an Alagad backpack (trust me, these are the best backpacks ever, I use mine for travel, school, everything). I will do a drawing for the backpack in a connect room after this blog series is completed, so lets bring on the idea’s!

Tag Cloud