Clone https://github.com/Tronil/rollup-ts-mem-leak
Run:
npm install
npm run watch
Repeatedly trigger watch build by saving src/index.ts
Memory usage should not grow until rollup crashes with out-of-memory.
Memory usage grows with about 25MB for every triggered build, eventually running out of memory and crashing.
Note that this repo only contains a "hello world" file - for real projects this memory leak is massive. In our project the leak was around 300 MB for every build, making the process crash after 6 or 7 builds. The 3.x versions do not appear to have this issue, so it is likely related to the 4.0 rewrite.
Have you verified that it's a problem with that plugin specifically and not rollup core?
If I use version 3.1.1 of the typescript plugin, the memory leak seems to disappear - so I assume the leak is in the plugin. Also the leak did not appear before adding the typescript plugin (our project was originally in pure javascript).
Looked through the source and this part caught my eye:
buildStart() {
emitParsedOptionsErrors(ts, this, parsedOptions);
program = createWatchProgram(ts, this, {
formatHost,
resolveModule,
parsedOptions,
writeFile(fileName, data) {
emittedFiles.set(fileName, data);
}
});
},
program is being replaced every time buildStart() is called, which looks a little odd. After I put in a check for whether it is already set, the leak disappears...
So this seems to fix it:
buildStart() {
emitParsedOptionsErrors(ts, this, parsedOptions);
if (!program) {
program = createWatchProgram(ts, this, {
formatHost,
resolveModule,
parsedOptions,
writeFile(fileName, data) {
emittedFiles.set(fileName, data);
}
});
}
},
Would you be interested in submitting a PR with the change? Otherwise I can add it in later.
I don't know when I'll have some time to look at a PR, so I wouldn't wait for me to create one.
I can make this PR.
Most helpful comment
Looked through the source and this part caught my eye:
programis being replaced every time buildStart() is called, which looks a little odd. After I put in a check for whether it is already set, the leak disappears...So this seems to fix it: