我有一个使用Surfaceview来显示UI的应用程序。该应用程序可稳定运行约18k用户,但是有3台设备返回到Surfaceview活动(SV活动->常规活动->返回SV活动)时会获得ANR。

这三种设备是:


昂达平板电脑(全志A31芯片组)
Sero 8(Rockchip芯片组)
带有英特尔凌动的Acer 10英寸平板电脑


我试图重新创建ANR,但失败了。根据我的用户,该应用可以正常运行数小时,除了上面列出的设备外,没有任何问题。

来自Android 4.2的ANR Stacktrace:

DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)

"main" prio=5 tid=1 WAIT
| group="main" sCount=1 dsCount=0 obj=0x41c899a0 self=0x41a6c010
| sysTid=5497 nice=0 sched=0/0 cgrp=apps handle=1074877404
| state=S schedstat=( 0 0 0 ) utm=541 stm=129 core=2
at java.lang.Object.wait(Native Method)
- waiting on <0x41c89da0> (a java.lang.VMThread) held by tid=1 (main)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:810)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:843)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1173)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:183)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:259)
at android.view.SurfaceView.updateWindow(SurfaceView.java:597)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:329)
at android.view.View.dispatchWindowVisibilityChanged(View.java:7544)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1039)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1002)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4400)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)

"SurfaceDraw" prio=5 tid=15 SUSPENDED
| group="main" sCount=1 dsCount=0 obj=0x42867d30 self=0x69a65b38
| sysTid=12101 nice=0 sched=0/0 cgrp=apps handle=1753253360
| state=S schedstat=( 0 0 0 ) utm=7914 stm=23 core=0
at android.graphics.Canvas.native_drawARGB(Native Method)
at android.graphics.Canvas.drawARGB(Canvas.java:801)
at com.davidgiga1993.mixingstationlibrary.surface.BaseSurface.b(BaseSurface.java:167)
at com.davidgiga1993.mixingstationlibrary.surface.k.run(DrawThread.java:27)

"AsyncTask #3" prio=5 tid=14 WAIT
| group="main" sCount=1 dsCount=0 obj=0x421734d0 self=0x690d59d8
| sysTid=5820 nice=0 sched=0/0 cgrp=apps handle=1762495280
| state=S schedstat=( 0 0 0 ) utm=0 stm=0 core=0
at java.lang.Object.wait(Native Method)
- waiting on <0x421735f0> (a java.lang.VMThread) held by tid=14 (AsyncTask #3)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)

"Binder_3" prio=5 tid=13 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4215a380 self=0x684f9540
| sysTid=5691 nice=0 sched=0/0 cgrp=apps handle=1693280728
| state=S schedstat=( 0 0 0 ) utm=3 stm=0 core=3
#00 pc 00016fe4 /system/lib/libc.so (__ioctl+8)
#01 pc 0002a75d /system/lib/libc.so (ioctl+16)
#02 pc 00016ba1 /system/lib/libbinder.so (android::IPCThreadState::talkWithDriver(bool)+132)
#03 pc 00017363 /system/lib/libbinder.so (android::IPCThreadState::joinThreadPool(bool)+154)
#04 pc 0001b15d /system/lib/libbinder.so
#05 pc 00011267 /system/lib/libutils.so (android::Thread::_threadLoop(void*)+114)
#06 pc 00046887 /system/lib/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell(void*)+66)
#07 pc 00010dcd /system/lib/libutils.so
#08 pc 0000e3d8 /system/lib/libc.so (__thread_entry+72)
#09 pc 0000dac4 /system/lib/libc.so (pthread_create+160)
at dalvik.system.NativeStart.run(Native Method)

"AsyncTask #2" prio=5 tid=12 WAIT
| group="main" sCount=1 dsCount=0 obj=0x42145450 self=0x64f1aac8
| sysTid=5525 nice=0 sched=0/0 cgrp=apps handle=1693560600
| state=S schedstat=( 0 0 0 ) utm=0 stm=0 core=3
at java.lang.Object.wait(Native Method)
- waiting on <0x421455c8> (a java.lang.VMThread) held by tid=12 (AsyncTask #2)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)

"AsyncTask #1" prio=5 tid=11 WAIT
| group="main" sCount=1 dsCount=0 obj=0x42141d40 self=0x64ef35e8
| sysTid=5524 nice=0 sched=0/0 cgrp=apps handle=1693143224
| state=S schedstat=( 0 0 0 ) utm=0 stm=0 core=2
at java.lang.Object.wait(Native Method)
- waiting on <0x42141ed8> (a java.lang.VMThread) held by tid=11 (AsyncTask #1)
at java.lang.Thread.parkFor(Thread.java:1231)
at sun.misc.Unsafe.park(Unsafe.java:323)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:159)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2019)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:413)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1013)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1073)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)


根据我的解释,ANR发生在surfaceCreated被调用之前。

这是Surfaceview和绘图线程的源代码:

public class BaseSurface extends SurfaceView implements SurfaceHolder.Callback
{
    protected SurfaceHolder holder;
    private DrawThread drawThread;

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        Log.d("Surface", "Changed");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        synchronized (holder)
        {
            this.holder = holder;

            if (drawThread != null)
            {
                drawThread.Active = false;
                try
                {
                    drawThread.join();
                }
                catch (InterruptedException e)
                {
                }
            }

            drawThread = new DrawThread(this);
            drawThread.Active = true;
            drawThread.start();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        synchronized (holder)
        {

            drawThread.Active = false;
            boolean retry = true;
            while (retry)
            {
                try
                {
                    drawThread.join();
                    retry = false;
                }
                catch (InterruptedException e)
                {
                }
            }

            drawThread = null;
            this.holder = null;
        }
    }

    public void Update()
    {
        if (holder == null)
            return;

        Canvas canvas = holder.lockCanvas();
        if (canvas != null)
        {
            synchronized (holder)
            {
                //drawing the ui...
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

public class DrawThread extends Thread
{
    public boolean Active = false;
    private BaseSurface surface;

    private long frameStartTime;
    public float FPS = 38f;// = 26fps; 27f = 37fps
    private int sleepTime;

    public DrawThread(BaseSurface surface)
    {
        super("SurfaceDraw");
        this.surface = surface;
    }

    @Override
    public void run()
    {
        while (Active)
        {
            frameStartTime = SystemClock.uptimeMillis();
            surface.Update();
            try
            {
                sleepTime = (int) (FPS - (SystemClock.uptimeMillis() - frameStartTime));
                if (sleepTime > 0 && sleepTime < 1000)
                {
                    Thread.sleep(sleepTime);
                }
            }
            catch (InterruptedException ex)
            {
                Log.d("DrawThread", "Interrupred");
            }
        }
        Log.d("DrawThread", "Finished");
    }
}


在过去的几天中,我已经进行了很多搜索,但是没有找到任何线索来说明这种情况的发生。
我发现的唯一类似问题是在这里:https://groups.google.com/forum/#!msg/android-developers/0VuqnrYe7b0/Yw1mHodmrwoJ,但是他没有发布任何解决方案,并且他的问题与特定设备无关。

在使用某些特定的设备之前,是否还有其他人遇到过这些问题,并且知道该问题的解决方案?

编辑:

我找到了一种随机再现ANR的方法。
当表面活动关闭时,会发生导致ANR的真正问题。
这是“好”关闭和“坏”关闭的堆栈跟踪:



04-24 14:54:10.798: D/DrawThread(1526): Finished
04-24 14:54:10.798: D/Surface(1526): surfaceDestroyed




04-24 14:54:16.851: D/DrawThread(1526): Finished
04-24 14:54:16.851: D/Surface(1526): surfaceDestroyed
04-24 14:54:16.860: E/SurfaceHolder(1526): Exception locking surface
04-24 14:54:16.860: E/SurfaceHolder(1526): java.lang.IllegalArgumentException
04-24 14:54:16.860: E/SurfaceHolder(1526):  at android.view.Surface.lockCanvasNative(Native Method)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at android.view.Surface.lockCanvas(Surface.java:76)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:744)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:720)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at com.davidgiga1993.mixingstationlibrary.surface.BaseSurface.Update(BaseSurface.java:169)
04-24 14:54:16.860: E/SurfaceHolder(1526):  at com.davidgiga1993.mixingstationlibrary.surface.DrawThread.run(DrawThread.java:27)
04-24 14:54:16.860: D/Surface(1526): surfaceCreated


为什么调用异常?在破坏表面之前停止拉制螺纹,并且不再有任何物体接触表面视图。另外,为什么要在该异常之后调用表面的surfaceCreated
此时,该活动甚至不再可见。

我也尝试删除所有同步块,但它们没有改变行为。

最佳答案

查看android-developers链接中的ANR跟踪,他们正在运行Android 4.2,并且当SurfaceView尝试锁定其Surface时,其主线程处于停止状态。我认为问题在于渲染线程调用了lockCanvas(),它锁定了Surface(使用ReentrantLock),然后发生了一些事情,导致SurfaceView需要更新(例如,其大小或位置发生了变化)。您可以在看起来有点复杂的Skia代码中(大概)称为lockCanvas()的线程的跟踪中看到它正在主动运行(“ Thread-3899”在state=R中是NATIVE)。因此,要么Skia代码永远循环,要么花了很长时间才能完成。

在您的情况下,渲染线程(SurfaceDraw)被挂起,可能是因为它完成了正在执行的操作,并且正在从本机代码返回到VM。您的电话很简单,所以我不确定为什么要花这么长时间。可能有其他原因使它停滞了,而这恰恰是ANR快照机制最终陷入困境的时候。

在调用lockCanvas()之前,最好先抓住SurfaceHolder上的锁,以确保在持有Canvas锁的情况下不会阻止等待它。

(FWIW,在SurfaceHolder实例上进行同步使我有些紧张,因为您不知道SurfaceView中的某些内容是否会出于自身的邪恶目的而将其锁定。不过,请不要以为这就是问题所在。)

关于android - 某些设备上的ANR内部功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23225993/

10-10 13:44