handlebars-lang / handlebars.js

Minimal templating on steroids.
http://handlebarsjs.com
MIT License
17.82k stars 2.04k forks source link

Memory leak when invoking compiled templates #1885

Closed AnirudhaGohokar closed 1 year ago

AnirudhaGohokar commented 1 year ago

Hi,

Invoking compiled templates is leaking memory in our nodejs service. We are able to reproduce by calling invoking compiled template of size 32kb for over 4000 iterations per minute. This hardly generates 150 mb data per min which should ideally be garbage collected but infact it doesnt and eventually after 5mins , memory utilisation goes over 1G.

This is how we are using the method:

const hbar = require("handlebars");
return {
        "template": hbar.compile(template, { strict: checkstrict })(templateData, { data: { intl: intlData } }),
        "subject": hbar.compile(subject, { strict: checkstrict })(templateData, { data: { intl: intlData } })
    }
AnirudhaGohokar commented 1 year ago

Memory leak gone after switching to mustache.js since its similar to handlebars. This confirms that there's a serious memory leak happening for > 32kb data. Please look into it.

AnirudhaGohokar commented 1 year ago

https://github.com/handlebars-lang/handlebars.js/issues/271#issue-5437561

jaylinski commented 1 year ago

I guess this also happens with the latest Nodejs release? (18.9.0)

AnirudhaGohokar commented 1 year ago

Hey, Yes i have tested it with latest node release (18.9.0). Issue is easy to reproduce by just iterating over the compile and rendering part for N iterations. Memory steadily rises over a period of time unless you stop the execution.

ccapndave commented 1 year ago

I'm also experiencing this, and its causing our servers to run out of memory and crash. The issue seemed to start when we upgraded from Node 14.20.1 to 16.17.1. There is a discussion at https://github.com/facebook/jest/issues/11956 which may be relevant.

It sounds like the problem could be something to do with the vm module.

ccapndave commented 1 year ago

This is absolutely a showstopper for us. Does anyone have any ideas of a fix or even a workaround?

I can confirm that downgrading to Node v16.10.0 does not fix the issue.

ccapndave commented 1 year ago

After a lot of fiddling about, I am extremely happy to say that the problem was actually to do with the way that we were bundling and running the app containing Handlebars, and not anything to do with Handlebars itself. Apologies if anyone has investigated this.

sergehuber commented 1 year ago

Maybe you could close this issue ?

ccapndave commented 1 year ago

From my side its fine (the memory leak was my fault and not Handlebar's) but I don't know if @AnirudhaGohokar was experiencing the same issue as me.

raukaute commented 4 days ago

After a lot of fiddling about, I am extremely happy to say that the problem was actually to do with the way that we were bundling and running the app containing Handlebars, and not anything to do with Handlebars itself. Apologies if anyone has investigated this.

Hey, can you provide a bit of details what was the cause in your case? Cheers