RxJava中的文件操作导致Android中文件描述符泄露

时间:2018-06-14 21:31:05

标签: android rx-java okhttp3

我开发Android应用程序,我想下载许多文件(大约1000个文件)。

它与Okhttp和RxJava一起使用。 但是app必须在循环中泄漏文件描述符大约200:

    AtomicInteger count = new AtomicInteger();
    Observable.fromArray(list.toArray(new String[list.size()]))//url list
            .subscribeOn(Schedulers.io())
            .doOnNext(url -> {
                Response response = request(url, null);
                String lastSegment = url.substring(url.lastIndexOf("/")+1);
                File file = new File(context.getFilesDir(), lastSegment);
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(response.body().bytes());
                fos.close();
                response.close();
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(url -> onProgress(stateCode, count.incrementAndGet(), list.size()),
                    this::handleError, this::onCompleteDl);

和错误日志:

06-15 06:26:52.204 22654-24804/{MY_PACKAEGENAME} A/libc: FORTIFY: FD_SET: file descriptor >= FD_SETSIZE
06-15 06:26:52.634 25745-25745/? A/google-breakpad: Microdump skipped (uninteresting)
06-15 06:26:52.794 22654-24804/{MY_PACKAEGENAME} A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 24804 (RxCachedThreadS)
06-15 06:26:52.854 18430-18430/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
    Build fingerprint: 'samsung/SC-04F/SC-04F:6.0.1/MMB29M/SC04FOMU1XQH1:user/release-keys'
    Revision: '13'
    ABI: 'arm'
    pid: 22654, tid: 24804, name: RxCachedThreadS  >>> com.cks.hiroyuki2.radiko <<<
    signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
06-15 06:26:53.024 18430-18430/? A/DEBUG: Abort message: 'FORTIFY: FD_SET: file descriptor >= FD_SETSIZE'
        r0 00000000  r1 000060e4  r2 00000006  r3 87b03978
        r4 87b03980  r5 87b03930  r6 0000000b  r7 0000010c
        r8 87afa6e4  r9 87afa778  sl 0000000a  fp 00000002
        ip 00000006  sp 87afa630  lr b6c8fc31  pc b6c92020  cpsr 40010010
06-15 06:26:53.044 18430-18430/? A/DEBUG: backtrace:
        #00 pc 00042020  /system/lib/libc.so (tgkill+12)
        #01 pc 0003fc2d  /system/lib/libc.so (pthread_kill+32)
        #02 pc 0001c3b3  /system/lib/libc.so (raise+10)
        #03 pc 00019631  /system/lib/libc.so (__libc_android_abort+34)
        #04 pc 00017584  /system/lib/libc.so (abort+4)
        #05 pc 0001afc7  /system/lib/libc.so (__libc_fatal+16)
        #06 pc 0001afdf  /system/lib/libc.so (__fortify_chk_fail+18)
        #07 pc 00045d85  /system/lib/libc.so (__FD_SET_chk+24)
        #08 pc 0000a4cf  /system/lib/libjavacrypto.so
        #09 pc 0000b0ab  /system/lib/libjavacrypto.so
        #10 pc 033ec46f  /system/framework/arm/boot.oat (offset 0x2f3a000)
06-15 06:27:00.724 18430-18430/? A/DEBUG: Tombstone written to: /data/tombstones/tombstone_02

我是RxJava的新手,所以我认为使用RxJava是错误的。 我该如何解决?有什么不对的?

提前致谢。

0 个答案:

没有答案