-
在 Android Studio 中,使用 Logcat 时,出现如下信息
WARNING: Logcat was not cleared on the device itself because 'logcat -c' timed out.
logcat: Unexpected EOF!This means that either the device shut down, logd crashed, or this instance of logcat was unable to read log
messages as quickly as they were being produced.If you have enabled significant logging, look into using the -G option to increase log buffer sizes.
问题原因
-
Android 设备的日志缓冲区被写满了,而且新日志生成的速度太快,导致 Logcat 读取跟不上,从而连接中断
-
报错的是 Android Studio,也就是 ADB 客户端,而不是设备本身
-
写入速度大于读取速度,生产者(设备)和消费者(Android Studio)之间出现了速度差
-
如果生产者写入新数据的速度极快,以至于在新数据覆盖旧数据的过程中,把 Android Studio 还没来得及读取的那段旧数据也给覆盖掉了,于是报错
-
处理策略
- 使用 ADB 清空现有日志,立即恢复
shell
adb logcat -c
补充学习
- 查看缓冲区大小
shell
adb logcat -g
# 输出结果
main: ring buffer is 16 MiB (597 KiB consumed), max entry is 5120 B, max payload is 4068 B
system: ring buffer is 16 MiB (256 KiB consumed), max entry is 5120 B, max payload is 4068 B
crash: ring buffer is 16 MiB (0 B consumed), max entry is 5120 B, max payload is 4068 B
kernel: ring buffer is 16 MiB (12 KiB consumed), max entry is 5120 B, max payload is 4068 B
- 修改缓冲区大小,例如,将缓冲区大小设为 4MB
shell
adb logcat -G 4m
-
缓冲区的"自清"
-
Android 的 logcat 缓冲区在底层是一个环形缓冲区
-
当新日志写入时,如果缓冲区还有剩余空间,就直接追加
-
当缓冲区完全写满时,新日志会从缓冲区的头部开始覆盖最旧的日志
-