最近做一个压力测试,打开关闭,打开关闭相机应用,当打开到200次左右的时候总是抛出下面的异常
03-06 11:48:03.220 5794 5908 E Parcel : dup() failed in Parcel::read, i is 1, fds[i] is -1, fd_count is 2, error:Too many open files
03-06 11:48:03.221 5794 952 E Fence : merge: sync_merge("SurfaceTexture-1-5794-223:3", -1, 1018) returned an error: Too many open files (-24)
03-06 11:48:03.389 5794 952 W Adreno-GSL: <gsl_ldd_control:475>: ioctl fd 50 code 0xc0140933 (IOCTL_KGSL_TIMESTAMP_EVENT) failed: errno 24 Too many open files
03-06 11:48:03.389 5794 952 W Adreno-GSL: <ioctl_kgsl_syncobj_create:2979>: (33, b, 4245) fail 24 Too many open files
日志看是文件打开太多导致的,但是我检查了所有关于文件操作的地方没有发现句柄没有释放的问题,
百思不得其解,搞了好久没有头绪。。
后来在网上看到一篇文章 http://blog.youkuaiyun.com/hehui1860/article/details/52402908 得到了启示。
如果创建的线程没有释放同样会句柄泄露
ps | grep "camera" 查到进程号
通过adb输入 ps -t | grep pid 可以查看到 进程里面创建的线程
u0_a108 14166 501 1772928 119532 SyS_epoll_ 00f712eee4 S com.android.camera
u0_a108 14171 14166 1772928 119532 do_sigtime 00f712f1b8 S Signal Catcher
u0_a108 14172 14166 1772928 119532 poll_sched 00f712f0d4 S JDWP
u0_a108 14173 14166 1772928 119532 futex_wait 00f7105648 S ReferenceQueueD
u0_a108 14174 14166 1772928 119532 futex_wait 00f7105648 S FinalizerDaemon
u0_a108 14175 14166 1772928 119532 futex_wait 00f7105648 S FinalizerWatchd
u0_a108 14176 14166 1772928 119532 futex_wait 00f7105648 S HeapTaskDaemon
u0_a108 14177 14166 1772928 119532 binder_thr 00f712f010 S Binder_1
u0_a108 14178 14166 1772928 119532 binder_thr 00f712f010 S Binder_2
u0_a108 14185 14166 1772928 119532 SyS_epoll_ 00f712eee4 S QHStatAgent
u0_a108 14186 14166 1772928 119532 SyS_epoll_ 00f712eee4 S QHStatAgent_Tim
u0_a108 14206 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 14208 14166 1772928 119532 SyS_epoll_ 00f712eee4 S view-assit-thre
u0_a108 14212 14166 1772928 119532 futex_wait 00f7105648 S pool-1-thread-1
u0_a108 14217 14166 1772928 119532 SyS_epoll_ 00f712eee4 S CameraHolder
u0_a108 14218 14166 1772928 119532 SyS_epoll_ 00f712eee4 S Camera Handler
u0_a108 14231 14166 1772928 119532 futex_wait 00f7105648 S Thread-1150
u0_a108 14232 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 14233 14166 1772928 119532 SyS_epoll_ 00f712eee4 S Thread-1153
u0_a108 14235 14166 1772928 119532 SyS_epoll_ 00f712eee4 S WifiManager
u0_a108 14241 14166 1772928 119532 SyS_epoll_ 00f712eee4 S RenderThread
u0_a108 14273 14166 1772928 119532 unix_strea 00f712ffe8 S TcmReceiver
u0_a108 14276 14166 1772928 119532 binder_thr 00f712f010 S Binder_3
u0_a108 14311 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #1
u0_a108 14336 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #2
u0_a108 14338 14166 1772928 119532 futex_wait 00f7105648 S thread-pool-0
u0_a108 14339 14166 1772928 119532 futex_wait 00f7105648 S thread-pool-1
u0_a108 14340 14166 1772928 119532 futex_wait 00f7105648 S thread-pool-2
u0_a108 14342 14166 1772928 119532 futex_wait 00f7105648 S thread-pool-3
u0_a108 14487 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #3
u0_a108 14583 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 14594 14166 1772928 119532 futex_wait 00f7105648 S Thread-1185
u0_a108 14595 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 14651 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #4
u0_a108 14748 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 14760 14166 1772928 119532 futex_wait 00f7105648 S Thread-1217
u0_a108 14761 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 14835 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #5
u0_a108 14910 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 14922 14166 1772928 119532 futex_wait 00f7105648 S Thread-1247
u0_a108 14923 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 14992 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #6
u0_a108 15045 14166 1772928 119532 binder_thr 00f712f010 S Binder_4
u0_a108 15072 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 15087 14166 1772928 119532 futex_wait 00f7105648 S Thread-1280
u0_a108 15091 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 15156 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #7
u0_a108 15233 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 15244 14166 1772928 119532 futex_wait 00f7105648 S Thread-1310
u0_a108 15245 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 15301 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #8
u0_a108 15392 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 15404 14166 1772928 119532 futex_wait 00f7105648 S Thread-1341
u0_a108 15405 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 15460 14166 1772928 119532 futex_wait 00f7105648 S AsyncTask #9
u0_a108 15556 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 15567 14166 1772928 119532 futex_wait 00f7105648 S Thread-1372
u0_a108 15568 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 15716 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 15727 14166 1772928 119532 futex_wait 00f7105648 S Thread-1402
u0_a108 15728 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 15879 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 15891 14166 1772928 119532 futex_wait 00f7105648 S Thread-1432
u0_a108 15892 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 16015 14166 1772928 119532 binder_thr 00f712f010 S Binder_5
u0_a108 16039 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 16051 14166 1772928 119532 futex_wait 00f7105648 S Thread-1463
u0_a108 16052 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 16202 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 16214 14166 1772928 119532 futex_wait 00f7105648 S Thread-1493
u0_a108 16215 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 16362 14166 1772928 119532 SyS_epoll_ 00f712eee4 S StartUpObjManag
u0_a108 16364 14166 1772928 119532 SyS_epoll_ 00f712eee4 S CameraSettingTh
u0_a108 16365 14166 1772928 119532 SyS_epoll_ 00f712eee4 S CameraUIBase
u0_a108 16366 14166 1772928 119532 SyS_epoll_ 00f712eee4 S Mode context Ha
u0_a108 16373 14166 1772928 119532 futex_wait 00f7105648 S Thread-1523
u0_a108 16375 14166 1772928 119532 SyS_epoll_ 00f712eee4 S PhotoPageManage
u0_a108 16376 14166 1772928 119532 SyS_epoll_ 00f712eee4 S BroadcastObjMan
u0_a108 16384 14166 1772928 119532 futex_wait 00f7105648 S GLThread 1526
u0_a108 16436 14166 1772928 119532 futex_wait 00f7105648 S Thread-1536
u0_a108 16441 14166 1772928 119532 SyS_epoll_ 00f712eee4 S SpecialEffectNo
u0_a108 16442 14166 1772928 119532 SyS_epoll_ 00f712eee4 S auto night
u0_a108 16460 14166 1772928 119532 SyS_epoll_ 00f712eee4 S toast
u0_a108 16470 14166 1772928 119532 futex_wait 00f7105648 S SoundPool
u0_a108 16471 14166 1772928 119532 futex_wait 00f7105648 S SoundPoolThread
果然 PhotoPageManage StartUpObjManag 这两个线程没有释放,一直在创建,找到原因后就好解决了。
以上是Android7.0以及之前版本的方式
Android8.0用一下方式查看
adb shell
ps -e | grep "camera" 查到进程号
cd /proc/pid/fd
ls -al
查看 82 -> anon_inode:dmabuf 之类的文件是否一致增多