ColdFusion: CFEXECUTE/FFMPEG Memory Issue?

1.1k Views Asked by At

So my application allows for users to upload video, convert it using FFMPEG and then transfer it over to the Flash Media Server. Lately, I've run into an issue.

If ever there is an error when converting video, I automatically generate cfcatch report PDF. This time I came across a "Cannot allocate memory" error. This massively concerns me because I'm about to promote my system out and I can't afford for the scripts to stop running within the first few hours.

Is there a way to clean up the memory issues with ColdFusion? I mean, once the job has been done, can I essentially "reset" the memory that the server was using?

If you understand the potential disaster, I'm sure you'll understand why I've got to find out how to make sure my scripts are executing properly. The physical fix is to restart the server, but I obviously cannot be restarting the server every single time a user uploads a video...

5

There are 5 best solutions below

4
On

I remember reading that some server versions don't properly dispose of COM objects and the like when a page request is finished. If any of this is being done via a CFC or Java class that's being set to a variable, you can put this in OnRequestEnd.cfm:

<cfset StructDelete(variables)>
<cfset StructDelete(request)>

This will get rid of any variables set on the page which are no longer needed. Shouldn't have any negative side effects, and should clear up any memory that might be still lurking in one of the variables set during the processing of that page.

You might also look into using something other than <cfexecute> to process the videos. Maybe have a background process that routinely checks a folder for videos and then converts them in the background? ColdFusion isn't necessarily efficient when it comes to batch processing.

1
On

I have come across some instances where it's helpful to actually manually run JVM garbage collection from within CF, usually when there is a long running thread doing long-term queue management and the request is very long running.

It might be worth a shot in your case.

To run the garbage collector from within CF you call the following:

<cfset runtime = CreateObject("java", "java.lang.Runtime").getRuntime()>
<cfset runtime.gc()>

Hope it helps!

0
On

If you are on windows, I recommend calling a batch file to do the conversion and file transfer. You can execute the Batch file from CF. This will prevent CF from consuming the entire memory for the conversion and the task can continue running in the background. If you want to wait to get the status, add a "timer" using a CF Java object instantiation to check the status after X seconds.

or you can call a cmd window to run it - http://www.forta.com/blog/index.cfm/2006/7/31/Using-CFEXECUTE-To-Execute-Command-Line-Utilities

1
On

Great answer but I couldn't get your script to work so re-touched and what a difference!!!

<cfloop collection="#REQUEST#" item="mydex">
    <cfset StructDelete(REQUEST, "#mydex#", "True")>
</cfloop>
<cfloop collection="#VARIABLES#" item="mydex">
    <cfset StructDelete(VARIABLES, "#mydex#", "True")>
</cfloop>
3
On

I don't understand why in the world you're trying to reinvent the wheel when I wrote a DSL wrapper for FFMPEG with the fix for the memory leak included in it:

https://github.com/rip747/cfffmpeg

Fork and submit any enhancements or fixes that you'd like.

BTW, if you want to see how to really handle the memory issues that you are having, then read the article by CFSEARCHING:

http://cfsearching.blogspot.com/2007/12/using-ffmpeg-to-convert-video-files-to.html

Again, this approach is included in the DSL.