Currently, I am using RazorEngine v2.1 as part of a background process that sends templated emails (thousands of them). To speed things up, the templates are compiled with their md5 sum as a name. This makes it so that when a template is changed, it is re-compiled and all emails using the template are able to use the same compiled template. I keep track of the names of the compiled templates in a list so that I know when to call compile again (and do a few other things).
The problem: It has occurred to me that after a really long time and after lots of template modifications that all these cached compiled templates will probably still be in memory since it looks like they are being stored in a dynamic
. For this particular process, which might run for months at a time without a restart, this could constitute a serious memory leak if all the previous versions of templates are still hanging around.
The question: Is there a way to un-cache old templates so that they are no longer hanging around in the dynamic
?
If, for example, I was able to keep the compiled template objects myself and just pass them into RazorEngine when I wanted to use them I could decide when to toss them out and that would eliminate the memory leak. However, if RazorEngine already has a way around this then knowing about that would be handy as well since I can't find very many references to this particular problem on the internet. There are plenty of things about why compiled templates should be used to reduce memory usage, but I had a hard time finding anything about tons and tons of unused compiled templates accumulating in a long-lived application.
EDIT: I have just read a bit about how the caching works and that if the same name is passed in with a different template it will re-cache it and discard the old one. The problem here still remains, however, since over the course of time emails will be added and removed and with time all the old removed emails will still be around (even though it won't be storing copies of each version of the template).