Parcel has bean finalizer Exception

[Android] Parcel has bean finalizer exception

调用UsageStats时遇到了此异常,下面是异常信息

10-28 21:09:22.872: E/System(16840): Uncaught exception thrown by finalizer
10-28 21:09:22.872: E/System(16840): java.lang.IllegalStateException: Binder has been finalized!
10-28 21:09:22.872: E/System(16840): at android.os.BinderProxy.transact(Native Method)
10-28 21:09:22.872: E/System(16840): at android.database.BulkCursorProxy.close(BulkCursorNative.java:288)
10-28 21:09:22.872: E/System(16840): at android.database.BulkCursorToCursorAdaptor.close(BulkCursorToCursorAdaptor.java:133)
10-28 21:09:22.872: E/System(16840): at android.database.CursorWrapper.close(CursorWrapper.java:49)
10-28 21:09:22.872: E/System(16840): at android.content.ContentResolver$CursorWrapperInner.close(ContentResolver.java:1591)
10-28 21:09:22.872: E/System(16840): at android.content.ContentResolver$CursorWrapperInner.finalize(ContentResolver.java:1604)
10-28 21:09:22.872: E/System(16840): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
10-28 21:09:22.872: E/System(16840): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)  
10-28 21:09:22.872: E/System(16840): at     java.lang.Thread.run(Thread.java:856)

此异常是因为Android在跨进程通信时系统为每个进程在Binder驱动中分配的内存是有限的大概10几兆,当我们短时间进行大量ipc时就会把Binder驱动的内存占满,而我们再进行ipc时就会出现此异常。

我的错误代码是

UsageStatsManager statsManager = (UsageStatsManager) ctx.getSystemService(USAGE_STATS_SERVICE);
    UsageEvents.Event event = new UsageEvents.Event();

    final long stepTimeMS = TimeUnit.HOURS.toMillis(1);
    long currBeginTimeMS = finalBeginTimeMS;
    long currEndTimeMS = currBeginTimeMS + stepTimeMS;

    while(isNeedScan) {
        final UsageEvents usageEvents = statsManager.queryEvents(currBeginTimeMS, currEndTimeMS);
        while (usageEvents.hasNextEvent()) {
            usageEvents.getNextEvent(event);

        }
    }

错误的根本原因是在While循环里做了大量的ipc数据获取耗尽了Binder驱动的内存

此问题发生在5.x

该问题是由于Binder驱动的代码错误导致的,当需要分配内存的时候无法分配,返回null抛出

0%