Skip to content
Mark Mandel edited this page Feb 3, 2012 · 1 revision

Due to the way classes are cached in ColdFusion, URLClassLoaders will not to be garbage collected. It is advised that instances of JavaLoader are stored in the Server scope, so that they never time out, and thus avoid this memory leak.

It should also be noted, that during development time (i.e. when recreating JavaLoader for new changes to take effect), there will be a leak into the PermGen memory space, so it may be required to restart ColdFusion occasionally to clear it. This will not be something that will effect a production application, as if JavaLoader is stored in the Server scope, it will never be removed.

An example of this, with proper locking would be:

//this is my unique key for my JavaLoader instance in the server scope
//please don't use this key for your own applications.
uuid = "22713350-0490-11df-8a39-0800200c9a66";
paths = getPathsToMyJars();

<cfif NOT StructKeyExists(server, uuid)>
    <cflock name="#application.applicationname#.server.JavaLoader" throwontimeout="true" timeout="60">
        <cfscript>
            if(NOT StructKeyExists(server, uuid))
            {
                server[uuid] = createObject("component", "javaloader.JavaLoader").init(paths);
            }
        </cfscript>
    </cflock>
</cfif>

This could also be done in onApplicationStart() as well, which would mitigate the need for locking, depending on how you architect your applications.

This could also be further encapsulated within a ColdFusion component method structure.

Clone this wiki locally