简体   繁体   中英

Android: Long running application stops responding. How to debug

I have a long running Service , it works perfectly, however, after a couple hours, it stops responding (it appears to be executing, but it isn't). I wish to find out why/how.

The failure/bug never happens "on cable", so debugging on my local desktop machine does not work.

I also written logs to disk, in order to follow the "flow" of the Service , but no failures happens ( MyService does Network work, at least once every 20 seconds, but may be much more)

I have a rooted Android device.

In this device, I use Wifi (IEE802.11) to communicate with an ESP8266.

For this, I have a Service, that runs indefinitely. I have "start" & "stop" actions to control it. It is started and stopped by the following:

MainActivity:

MyServices mMyServices = null;
boolean mIsBound = false;

onCreate(){
   Intent myIntent = new Intent(this, MyServices.class);
   startService(myIntent);
   bindService(
           myIntent,
           myServiceConnection,
           Context.BIND_AUTO_CREATE
   );
}

onDestroy(){
   unbindService(myServiceConnection);
   stopService(new Intent(this, MyServices.class));
}

private final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MyServices.MyBinder binder = (MyServices.MyBinder) service;
        mMyServices = binder.getService();
        mIsBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mIsBound = false;
    }
};

MyService is over 3k lines long, it has no blocking operations on itself, but holds Thread s that do, they seem to work correctly, even when the MyService stops responding to "Bound Activities".

MyService:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

private final IBinder mBinder = new MyBinder();
public class MyBinder extends Binder {
    public MyServices getService() {
        return MyServices.this;
    }
}

As far as I can tell, when the "bug" happens, is when I bind an Activity (several may be bound at once), then goes to background, after a couple hours, when a "Bound Activity" that was not on foreground goes to foreground, nothing happens, no Logs, no error, application just hangs, and ANR (Application Not Responding) message appears, and prompts to kill the application.

Update 25/10/2016

All binds have been removed and tested. The Service will halt operations, with no signs of the source of the failure at around 40 ~ 500 minutes. The device does a "exception happened" sound, its a Samsung SM-T810 tablet, nothing gets written to logs, no DDMS stacktrace, no path to follow...

I do realise that the Service "crashed", its Threads and other parts will continue to work properly. It appears no code on the Service Object runs, attempts to writte to disk never succed. Debugging via cable has not yelded any usefull information on logs over 1 mb long...

I am attempting to monitor the device via Wifi adb. As soon as it completes, will post the results

An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further events, and thus throws up an ANR dialog.

To identify which part of code is blocking the main thread ,you can pull the traces.txt in that each thread state & stack trace details available.

adb pull /data/anr/traces.txt

How to avoid ANR

https://developer.android.com/training/articles/perf-anr.html

below tools also can help to trace it.

Systrace

https://developer.android.com/studio/profile/systrace-commandline.html

TraceView

https://developer.android.com/studio/profile/traceview.html

Anr tracing:

Interpreting an ANR stack trace

I suggest you to create a text file with a Log of your desired messages to try to find the point where it crashes, or even write the Exception output there if possible. Check this thread where someone mentions how to write text file Log and then use it to write checkpoints in your running service. It's not a final solution or a debugging, but logging helps in many cases.

Hope it helps!

MainActivity stops your Service when it gets destroyed. Just unbind it in onDestroy so the Service keeps running. Please have a look at this example . The example also suggest to use onStart and onStop rather than onCreate and onDestroy to bind and unbind your service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM