简体   繁体   English

Chokidar Node.js package 从目录中的预先存在的文件中不必要地使用大量内存/RAM

[英]Massive memory/RAM usage with Chokidar Node.js package from pre-existing files in directory being watched unnecessarily

I'm using the Node.js package "chokidar" for this use case:我在这个用例中使用了 Node.js package “chokidar”:

  • I'm just watching a single directory on Linux, not recursively我只是在看 Linux 上的单个目录,而不是递归的
  • I only need it to watch for add events, for when files are atomically moved into the watched directory (they're moved from another directory on the same filesystem once all changes are done)我只需要它来监视add事件,因为当文件以原子方式移动到监视目录时(一旦所有更改完成,它们就会从同一文件系统上的另一个目录移动)
  • I don't need it to look at pre-existing files in the dir at all, just watch for new ones since the app started我根本不需要它来查看目录中预先存在的文件,只需注意应用程序启动后的新文件

Problem:问题:

  • When there is a large number of pre-existing files (like 80k) in the directory when I start my node.js app, Chokidar seems to put a watch on all of them, even though I don't care about watching existing files at all当我启动 node.js 应用程序时目录中有大量预先存在的文件(如 80k)时,Chokidar 似乎对所有文件都进行了监视,即使我根本不关心查看现有文件
  • Initially this was hitting the kernel watch limit, but I've already solved that with:最初这达到了 kernel 手表限制,但我已经通过以下方式解决了这个问题:

... ...

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
  • The remaining issue is that all these pre-existing files are being watched, which uses a massive amount of RAM for no reason, as I don't need it watching those files at all.剩下的问题是所有这些预先存在的文件都在被监视,它无缘无故地使用了大量的 RAM,因为我根本不需要它来监视这些文件。
  • Is there any way to have Chokidar just entirely ignore all these pre-existing files, and do nothing but watch this single directory for add events for new files only?有什么方法可以让 Chokidar 完全忽略所有这些预先存在的文件,并且只关注这个单个目录以仅为新文件add事件?
  • I know about increasing Node's RAM limits (which didn't work in this case anyway), but I feel like it shouldn't be using all this RAM in the first place, and I want it to be efficient on a small VPS.我知道增加 Node 的 RAM 限制(在这种情况下无论如何都不起作用),但我觉得它不应该首先使用所有这些 RAM,我希望它在小型 VPS 上高效。 I'd like to solve the RAM usage issue rather than just give it more RAM than it should need in the first place.我想解决 RAM 使用问题,而不是一开始就给它更多的 RAM。

I'm using this code:我正在使用这段代码:

chokidar.watch("/my-watched-dir", {
    ignoreInitial: true,
})
.on('add', (filepath) => {...}

I've also tried setting the depth option to 0 and 1 too.我也尝试将depth选项设置为 0 和 1。

The memory usage climbs very high as soon as the app starts (even before the 1st new file appears triggering the add event for the first time).应用程序一启动,memory 的使用率就会攀升得非常高(甚至在第一个新文件出现之前第一次触发add事件)。

And there's no problem when the number of pre-existing files is smaller, so it's not an issue related to through-put of new files after the app starts.并且预存文件数较少时也没有问题,所以不存在应用启动后新文件吞吐量的问题。

As far as I know, libraries like chokidar on Linux platforms will directly use fs.watch and fs.watchFile provided by Node.js.据我所知,Linux平台上的chokidar等库会直接使用Node.js提供的fs.watch和fs.watchFile。

To be cross-platform, these two APIs always listen for all events related to paths, so the answer is that you can't use chokidar for your purposes.为了跨平台,这两个 API 始终侦听与路径相关的所有事件,因此答案是您不能将 chokidar 用于您的目的。

If you wish to use less memory, either poll manually or use a native Linux module that has direct access to inotify.如果您希望使用更少的 memory,请手动轮询或使用可以直接访问 inotify 的本机 Linux 模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM