paulmillr / chokidar

Minimal and efficient cross-platform file watching library
https://paulmillr.com
MIT License
10.86k stars 575 forks source link

JavaScript heap out of memory Alpine Linux #1268

Closed songp-pi closed 2 months ago

songp-pi commented 1 year ago

Describe the bug JavaScript heap out of memory

Screenshot 2023-02-02 at 17 31 21

Versions:

To Reproduce:

I create 250000 files in FILE_DIR and get the memory leak, this code just log the messages And the memory growth over time when create more files

const test = () => {
  const watcher = chokidar.watch([
    FILE_DIR
  ], {
    awaitWriteFinish: true,
    ignored: [
      `${FILE_DIR}/*.txt`,
    ],
    persistent: true,
    ignoreInitial: true,
    usePolling: true,
  });
  watcher
    .on('all', (event, path) => {
      console.log(`File ${path} has been ${event}`);
    })
}
test();
patricknelson commented 6 months ago

Found that this occurs even with a much smaller number of files, particularly if a large set of them happen to change all at once (e.g. ~180 or so in my case, all of them .css and .map files in only like 4 subdirectories, including the root one). That said, I found that removing the nested css/**/*.css glob pattern alleviates the issue. Give that a shot.

You can test this by checking memory usage live as it's running. I do this by running the script in debug mode via:

NODE_OPTIONS='--inspect' node test.js

Then open Chrome DevTools, click the Node.js logo that comes up and then open the "Memory" tab. There you'll see this below. For me, it always starts out at 5MB. However, with the nested subdirectory glob pattern **/ it will bloat until the VM runs out of memory (>2GB). But if I just explicitly watch *.css in each of the 4 explicit directories, it bloats to maybe 150MB at most, hovers around 70MB but eventually falls back to like 5MB.

image


Edit: #1271 does a fantastic breakdown and I think that's actually what is happening here.

SebastianPfliegel commented 4 months ago

But if I just explicitly watch *.css in each of the 4 explicit directories, it bloats to maybe 150MB at most, hovers around 70MB but eventually falls back to like 5MB.

The * wildcard had the same effect for us. We had to get rid of all globs/wildcards for not having the effect that the application allocates too much memory. Already 800 files at the same time exceeded the 1500MB max memory.