Streaming CF's output buffer to filestream to reduce OutOfMemory issues

I’m working to resolve an issue in our application which was leading to JVM memory issues, because the page can end up generating a very large amount of data, which can lead to OutOfMemory issues.

That got me thinking that it would be useful if there was a way for Lucee to start buffering it’s output to the file system if the buffer stream reached a certain threshold and then when time to send the content upstream, it would read from the file stream.

The goal would be to add safe guards against memory issues on pages that could generated 100s of MBs of data, so instead of the content buffer trying to put it all in a StringBuilder it could write directly to disk after it hits a certain threshold (maybe 10MBs, or whatever the developer finds appropriate). While this obviously would have some performance issues streaming to disk, for requests generating a large amount of data, that performance hit isn’t really going to matter.

In our use case, we cannot use <cfflush />, because the output is generated via backend process because the content can take minutes to generate.

Perhaps there are better ways to safe guard from these types of issues, but I thought I’d offer up a proposal.

There’s a discussion about this on Slack too:

as I said on slack, I’d be just writing out to a file.

That’s what I do… I prefer to take an API approach for files that take a long time to generate and use CFThread and return a UUID. The data is then written to a file. The web app then performs ajax requests to determine if file generation has been completed, if so, an automatic download is performed (or we can display a notification w/link). This approach has worked so much better as I can even cache the results and return it if a duplicate request is performed within X minutes.

1 Like