[英]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”:
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
事件,因为当文件以原子方式移动到监视目录时(一旦所有更改完成,它们就会从同一文件系统上的另一个目录移动) ... ...
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
add
events for new files only?add
事件?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.