Memory leak in gulp.watch leaving vinyl‑fs fs.ReadStream instances unclosed
0 reputation · 28 Jun 2026, 11:30 UTC
0 reputation · 28 Jun 2026, 11:30 UTC
The goal is to confirm whether the memory‑leak fix introduced in gulp 4.0.2 fully resolves resident‑memory growth when gulp.watch monitors thousands of files with a short polling interval and the ignoreInitial option set to false.
Although the fix ensures that each watch call destroys the underlying chokidar watcher and calls stream.destroy() on vinyl‑fs streams, reports indicate that aggressive polling or large directory trees can still cause elevated memory usage if the watcher holds onto numerous change events, and third‑party plugins that manage their own streams may bypass the cleanup.
Does enabling a polling interval of 100 ms with ignoreInitial:false lead to a measurable increase in heap size over a 10‑minute run? Does the retention of change events prevent garbage collection of vinyl‑fs streams even after stream.destroy() is called? Are there specific plugin patterns that are known to re‑introduce similar leaks despite the core fix?
Enabling a polling interval of 100 ms with ignoreInitial:false can lead to a measurable increase in heap size over a 10‑minute run when gulp.watch monitors many files, but the growth is mainly caused by retained change events or third‑party plugins that keep their own stream references, not by a failure of the core stream.destroy() fix.
stream.destroy() on the underlying vinyl‑fs fs.ReadStream objects and destroys the chokidar watcher.stream.destroy() is called and no other references exist, the stream becomes eligible for garbage collection.fs.ReadStream objects stay reachable, preventing GC even after destroy().gulp.src() and storing the returned stream) or wrap streams without properly ending them, re‑introducing the leak despite the core fix.gulp.watch('**/*', { polling: true, interval: 100, ignoreInitial: false }, (cb) => { console.log('change'); cb(); });--inspect or use clinic.js/heapdump to capture heap snapshots before starting the watch, after 5 minutes, and after 10 minutes.ReadStream objects; a steady increase indicates a leak.gulp.src() in a global array and observe whether heap growth accelerates.To refine the recommendation, please confirm the approximate number of files (and directory depth) you are watching with gulp.watch.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.