JavaLoader and Memory Consumption

If you are using JavaLoader to load external Java classes in ColdFusion, please be sure you are aware of the possible impact on server memory. From the JavaLoader documentation:

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.

For more on this topic please see this post, Using a Java URLClassLoader in CFMX Can Cause a Memory Leak, as well as the JavaLoader documentation on this subject, JavaLoader 1.0 Beta 2.

This entry was posted in Development and tagged , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.

2 Responses to JavaLoader and Memory Consumption

  1. Jamie Ly says:

    Good to know. I will keep this in mind if I have to use a Java Class! One question though, after a bit of Googling, I didn’t see how to use the Server scope. Is it as simple as adding a key to a Server struct? This kind of thing seems like it would be dangerous and require coordination between all stakeholders in the relevant server cluster. (If I’m correctly guessing how the scope works.)

  2. Nathan Mische says:

    The JavaLoader documentation I link to above has a good example of how to write to the server scope, with proper locking. In short yes, you just add a key to the server struct, just like any other scope. You can find out more about the server scope here: (Using server variables) And yes, you are correct that you have to be careful when writing to the server scope as you could easily overwrite someone else’s variable. However, server variables are limited to a given server instance and are not cluster aware. Again, the JavaLoader documentation shows using a UUID as the variable name to hold your JavaLoader instance which is probably a good idea.One final note, this isn’t an issue using classes already on the ColdFusion server’s classpath. In that case you can just use CreateObject”java”,…. JavaLoader is used to dynamically load classes at runtime, useful if you want to use a package not installed with ColdFusion, or if you want/need to use multiple versions of a library.

Leave a Reply

University of Pennsylvania Logo
Copyright © 2015 The Wharton School, University of Pennsylvania