Rip's Domain

Help: Force ColdFusion to release memory.

Posted in ColdFusion by rip747 on September 29, 2008

Need some help. Anyone out there know of a why to force ColdFusion to release memory back to the JVM?

The situtation is that I have a conversion script that creates a bunch of objects to pull data from one database to another. The memory keeps spiking after a 1000 or so inserts and i’ve tried everything to get to do garbage collection and release the memory back to the jvm.

Things i’ve tried

Invoking garbage collection with java.lang.system

<cfset loc.sys = createobject(“java”, “java.lang.System”)>
<cfset loc.sys.runFinalization()>
<cfset loc.sys.gc()>

setting the objects to javacast(“null”, “”)

Invoking garbage collection with java.lang.runtime

<cfset loc.sys = createobject(“java”, “java.lang.runtime”).getruntime()>
<cfset loc.sys.gc()>

I’ve even tried using java.lang.ref.SoftReference

Plus a combination of all the above!!!!

Nothing I seem to do want to make the jvm run the garbage collection and release the memory.

If anyone has an itelligent suggestion, leave it here. I’m at my whits end!!!

BTW please don’t say to use queries or split up the job. That’s not an answer I’m even looking for.

9 Responses

Subscribe to comments with RSS.

  1. Sumit said, on September 30, 2008 at 10:23 am

    I’m not familiar with ColdFusion at all but I do a lot of Java development. The way garbage collection works, in any memory managed language, is it seeks objects that are in memory with no references to them; so there would be no way to actually call said object.

    Memory managed systems have tables to keep track of these references but they can still suffer from memory leaks. How? Imagine you have a double linked list where object A holds a reference to object B and object B also holds a reference back to object A. If your application just points to object A and you set that reference to null, A and B still refer to each other and therefore will never get garbage collected, even those there is no path to either object through the running application.

    You may want to check your own code to see if any of this type of cross referencing exists. There may also be leaks in the database or ColdFusion libraries you are using themselves. Often these types of bugs are hard to find without the use of some kind of memory debugger. Are you running on top of an application server like TomCat, BEA Weblogic or IBM WebSphere?

    You may need to find a memory debugging application to find where the leaks are occurring; spy out all the user created objects in memory and see if there are some that aren’t disappearing like they should.


  2. Mark said, on February 16, 2009 at 2:33 am

    I have gc() working (without or setting the objects to javacast(”null”, “”).

    I can see it working in the Memory Usage Summary page of the Performance Monitor.

  3. Mark said, on February 16, 2009 at 2:43 am

    I have gc() working (without
    cfset loc.sys.runFinalization()
    or setting the objects to javacast(”null”, “”).

    I can see it working in the Memory Usage Summary page of the Performance Monitor.

  4. Mark Landmann said, on March 2, 2009 at 5:33 pm

    Probably too late to help, but here is what helped me:

    Add this to the application.cfc:

  5. Mark Landmann said, on March 2, 2009 at 5:34 pm

    %lt;cfset structClear(variables) />

  6. Randy said, on July 24, 2009 at 11:38 am

    I looked at a dump of variables and on the site I am working on and all that was in there was functions I created and a couple variables that I had set in the application file. I am not sure how clearing those only to be recreated on the next page request / refresh is gonna help things. Does it internally launch the garbage collector?

  7. Travis Heeter said, on January 24, 2011 at 12:44 pm

    I had the same problem when I was working on a similar program.

    For some reason – I don’t know what that reason is – Coldfusion does not dump variable content when a loop finishes, so the heap grows until the program crashes. You need to scope your variables. The easiest way to do this is to just put all your code in a function, and call that function outside of the function. Inside the function you need to initialize ALL your variables with var. Also, you should check out varscoper here it will tell you which variables need to be scoped, and will even give you the code necessary to accomplish that. Initializing your variables with var can only be done inside a function for some reason, but it forces CF to dump the content of the variable every iteration. Here’s an example:

    select id from products


    select image_url from images where site_id=#id#


  8. Travis Heeter said, on January 24, 2011 at 12:52 pm

    Oh hey, it took out all the CF code. Let me try that again.

    cffunction name=”main” access=”private” returntype=”any”
    cfset var some_variable = ”
    cfset var some_query = ”

    cfquery name=”some_query” datasource=”ds”
    select id from products

    cfloop query=”some_query”
    cfset some_variable=inner_query(id)

    cffunction name=”inner_query” access=”private” returntype=”any”
    cfargument name=”id” type=”any” default=””

    cfquery name=”getURL” datasource=ds”
    select image_url from images where product_id=#id#

    cfoutput #main()# /cfoutput

  9. Sean Harrison said, on October 24, 2011 at 12:44 pm

    freeMemoryThreshold = 200;
    objRunTime = createObject(‘java’, ‘java.lang.Runtime’).getRuntime();
    theFreeMemory = ((objRunTime.freeMemory() / 1024) / 1024);
    if(theFreeMemory LT freeMemoryThreshold){
    objSystem = createObject(‘java’, ‘java.lang.System’);
    objSystem = javacast(‘null’, ”);
    objRuntime = javacast(‘null’, ”);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: