External file changes sync slow: The current inotify limit is too low

本文详细介绍了如何解决在使用Android Studio时遇到的文件变化同步缓慢的问题,通过调整inotify watch limit设置,确保开发环境运行顺畅。

https://robbinespu.github.io/eng/2018/08/25/slow_file_changes_sync_inotify_limits.html

I get this message when waiting gradle task on my Android Studio 3

External file changes sync may be slow: The current inotify(7) watch limit is too low

WTF is this? After some google-fu , I found some official instruction from JetBrain. You can read here for more details.

Now, let’s check the current setting:

$ cat /proc/sys/fs/inotify/max_user_watches
8192

Does the current value are too small? Add new conf file..

$ sudo touch /etc/sysctl.d/60-jetbrains.conf

Open the file and add this lines

# Set inotify watch limit high enough for IntelliJ IDEA (PhpStorm, PyCharm, RubyMine, WebStorm).
# Create this file as /etc/sysctl.d/60-jetbrains.conf (Debian, Ubuntu), and
# run `sudo service procps start` or reboot.
# Source: https://confluence.jetbrains.com/display/IDEADEV/Inotify+Watches+Limit
# 
# More information resources:
# -$ man inotify # manpage
# -$ man sysctl.conf # manpage
# -$ cat /proc/sys/fs/inotify/max_user_watches # print current value in use

fs.inotify.max_user_watches = 524288

Let’s restart the systemd

$ sudo sysctl -p --system

Now, restart you IDE. It should be no complaint anymore ;d

 

在Linux系统中,`inotify` 是一种用于监控文件系统变化的机制。当使用如 Dropbox 等工具时,可能会遇到 `inotify` 实例或观察者数量达到上限的问题,表现为错误信息如“Unable to monitor filesystem”或“No space left on device - Failed to watch”。这类问题通常与 `inotify` 的用户限制有关。 ### inotify 限制的解决方案 #### 1. **调整 `max_user_watches`** - `inotify` 的核心限制之一是每个用户可以创建的 `watch` 数量上限,这个值由 `/proc/sys/fs/inotify/max_user_watches` 控制。 - 如果监控的目录数量过多(例如 Dropbox 监控大量文件夹),可能会超过默认限制。 - 可以通过以下命令临时增加该限制: ```bash echo 100000 | sudo tee /proc/sys/fs/inotify/max_user_watches ``` - 为了使更改永久生效,可以在 `/etc/sysctl.conf` 文件中添加以下行: ```bash fs.inotify.max_user_watches=100000 ``` - 然后运行 `sysctl -p` 应用配置[^1]。 #### 2. **调整 `max_user_instances`** - 每个用户可以创建的 `inotify` 实例数量由 `/proc/sys/fs/inotify/max_user_instances` 控制。 - 如果多个程序同时使用 `inotify`,可能会达到此限制。 - 调整方法类似,可以通过以下命令临时修改: ```bash echo 8192 | sudo tee /proc/sys/fs/inotify/max_user_instances ``` - 同样,若需永久生效,可在 `/etc/sysctl.conf` 中添加: ```bash fs.inotify.max_user_instances=8192 ``` #### 3. **调整文件描述符限制** - 如果遇到“Too many open files”的问题,则可能是达到了系统的文件描述符限制。 - 文件描述符限制分为软限制和硬限制,可以通过 `ulimit` 命令查看当前设置: ```bash ulimit -n # 查看当前打开文件数限制 ulimit -u # 查看最大进程数限制 ``` - 若要提高限制,可以编辑 `/etc/security/limits.conf` 文件,添加如下内容(假设用户名为 `user`): ```bash user soft nofile 65536 user hard nofile 65536 ``` - 对于系统范围的限制,可修改 `/etc/pam.d/common-session` 和 `/etc/pam.d/common-session-noninteractive`,添加: ```bash session required pam_limits.so ``` - 此外,还需确保 `/etc/sysctl.conf` 中包含以下内容以优化内核级别的文件描述符限制: ```bash fs.file-max = 2097152 ``` #### 4. **检查并优化应用程序行为** - 某些应用程序可能不必要地创建了大量 `inotify` 观察点,或者未能正确释放资源。 - 例如,Dropbox、IDE(如 PhpStorm)、日志监控工具等都可能频繁使用 `inotify`。 - 可以尝试关闭不必要的文件监控功能,或减少监控的目录层级。 - 如果使用 PM2 运行 Node.js 或 PHP 应用程序(如引用[2]中的配置),应确保其不会无限制地生成子进程或打开过多文件描述符[^2]。 #### 5. **重启相关服务** - 修改完 `inotify` 参数后,某些服务(如 Dropbox)需要重启才能应用新的限制。 - 可以使用以下命令重启 Dropbox: ```bash dropbox stop && dropbox start ``` ### 总结 解决 `inotify` 实例限制或“Too many open files”问题的核心步骤包括: - 增加 `max_user_watches` 和 `max_user_instances` 的值; - 提高文件描述符限制(软限制和硬限制); - 优化应用程序行为,避免不必要的资源占用; - 重启相关服务以确保新配置生效。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值