ColdFusion Muse

Variables Scope Memory Leak (say it ain't so)

I just read this fabulous post from Mike Schierberl's blog on Variables Scope Leaking and I thought I would pass it on as a good read. To summarize, it is a common practice to create a component into the application scope and include an "init()" type function in it that returns an instance of itself. For example you might have an "employee" component who's "init()" function takes an employee id as an argument and returns an instance of "employee" populated with user data. That's pretty standard. If you return this new populated instance into the variables scope you would expect the variable to be discarded at the end of the request. Mike's post shows that this does not happen as expected. Because the variables returned are referenced (as apposed to "by value") they persist beyond the request termination.

As a fix you can add structClear(variables) to the end of your request - in the onRequestEnd() function for example. I can't explain it better than Mike. He includes a sample and a flash movie of how he came to his conclusions. It's very thorough.

New Information From Robi Sen

Robi Sen ran his own tests against the SUN JVM. He contests the assertion that this is a Coldfusion issue and he's focused on the JVM. His results may be found

  • Share:


  • Sean Corfield's Gravatar
    Posted By
    Sean Corfield | 11/6/06 11:50 PM
    If this memory leak was real, every single modern ColdFusion program would crash every few minutes. I'll be very interested to read the truth behind this when it finally gets out - Robi's site is down right now.
  • Robi Sen's Gravatar
    Posted By
    Robi Sen | 11/16/06 8:23 AM
    I don't think this is a issue with Var scope but there are enough reports of similar issues that make me suspect that some system combination or some other set of factors can create this issue.
    Now I did some more intense and in depth testing on three different servers with different hardware profiles as well as testing on different versions of CF with different VM's and could not recreate the memory leaks that Mike was getting.*

    Also several associates tried his tests on diverse systems and where not able to recreate his issues. He though is still having a issue and he has proven to me, at least with out me actually seeing his setup, that it is not his VM. I also do not think it is his JVM settings. It would be cool though to be able to get access to his systems and see how they are setup in that he has implied to me that the problem is happening on all his server instances. Very odd but I do think the statement on your part that if this was a issue with CF many more people would have seen it by now.

    * Note: People should always test on multiple systems, hardware profiles, and in virgin installs that reflect actual production systems not your development machine. If you can not afford new hardware consider using VMWare to create clean environments for functional testing and profiling things like memory usage.