Memory leaks with Coldfusion 8

For the past few weeks I've been incognito due to some memory leak issues we've been having with our new app. It is essentially a rewrite of our existing web application using Object Oriented techniques and embraces the Model-View-Controller paradigm. It really is a pretty little thing but unfortunately she crumbled under the weight of the world wide web. Anyone who has faced the memory leak problem will know that it is not an easy one to crack and it takes time to determine the exact issue. There are a lot of articles around that have excellent information which were all very helpful in my quest for a solution.

The system made a lot of use of the session and application scope for caching objects which was the first issue that we had to overcome. It seemed like a good idea, you've got loads of memory, you have a finite amount of objects that can be called for each session why not persist them. The problem is that ColdFusion 8 seems to have an issue with complex objects (i.e. CFCs) stored in the session and application scopes. The reason I say this is that we were seeing a very, very strange thing happening to the memory on our server. When load testing was run, the server was fine, the memory steadily increased until the initial sessions began expiring and then the average of memory usage plateaued, which is what you like to see. However, once the load test ended the memory steadily increased until it crashed the CF instance. Weird right? And so the search for a solution began. I came across a few helpful articles:

http://www.ghidinelli.com/2009/07/16/finding-memory-leaks-coldfusion-jvm

http://www.schierberl.com/cfblog/index.cfm/2006/10/12/ColdFusion_memoryLeak_profiler

http://www.alagad.com/blog/post.cfm/troubleshooting-coldfusion-performance-analysis-part-ii

These were tremendously helpful in assisting me in finding the solution. The last post from Alagad was the one that helped cure my issue in the end. You need to ensure that when making copies of persistent scoped variables that they are deep copied or used in a local scope within CFC functions. However, bear in mind that there are many causes for memory leaks and you should spend the time in figuring out the exact cause of your problem. I found the MAT tool supremely useful.

Here are some tips for curing memory issues:

  1. Install cumulative hotfix 4 for ColdFusion 8.0.1 (Get it here)
  2. Update the JVM to the latest provided by Sun, in my case it was JDK 1.6.0_18 (Get it here)
  3. Add the -XX:+AggressiveHeap option to the JVM configuration (Read about it here)
  4. Set your min and max heap to the same size to reduce the number of garbage collection calls
  5. Run varscoper against your code and ensure all local variables are var scoped (Get it here)
  6. Limit the use of variables scope within objects, use the THIS reference instead
  7. Limit the number of complex objects (cfcs) being stored in SESSION and APPLICATION scopes
  8. Clear the variables scope onRequestEnd (this doesn't really work when using Application.cfc though)
  9. Run load tests! We used WCAT for Windows (Get it here)
  10. Turn off CF monitoring use FusionReactor (or something similar) (Get FusionReactor here)

Hopefully these will help someone else going forward.

UPDATE 16/05/2010: Point #6 should be, ensure you scope your variables correctly. The THIS scope has its own set of limitations. Ensure that all variables (including loop indices) have the proper scope to avoid leakage.

Related Posts

This entry was posted in ColdFusion, General, Programming, Web 2.0 and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.