我有一个非常简单的应用程序,主要威胁启动2 AsyncTasks。 2个AsyncTasks几乎相同,这是我的一个AsyncTasks的代码:
private class waitPlayer extends AsyncTask<Integer, Integer, Void> {
@Override
protected Void doInBackground(Integer... params) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
CreateLog.addToLog(e.toString);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Update your layout here
super.onPostExecute(result);
setCanPlay(true, lastPlayerPlayed);
}
@Override
protected void onProgressUpdate(Integer... progress) {
//progress_p1.setProgress((int) progress[0]);
//progress_p2.setProgress((int) progress[0]);
}
}
AsyncTask唯一能做的就是等待200ms,完成后它会调用一个方法。该方法将再次调用AsyncTask。另一个AsyncTask几乎相同,只等待3000ms并调用另一种方法。
问题是我添加了第二个AsyncTask后,我的应用程序变得非常慢。它在AsyncTasks运行时冻结几秒钟。我的Logcat说充满了垃圾收集器通知。
我的手机没有冻结,只有应用程序。我有大约200mb的免费RAM。
01-06 19:26:00.695: W/ActivityThread(25739): Application com.obattech.twoplayergame is waiting for the debugger on port 8100...
01-06 19:26:00.705: I/System.out(25739): Sending WAIT chunk
01-06 19:26:00.710: I/dalvikvm(25739): Debugger is active
01-06 19:26:00.905: I/System.out(25739): Debugger has connected
01-06 19:26:00.905: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.105: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.305: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.505: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.705: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:01.910: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.110: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.310: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.510: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.710: I/System.out(25739): waiting for debugger to settle...
01-06 19:26:02.910: I/System.out(25739): debugger has settled (1351)
01-06 19:26:03.095: D/libEGL(25739): loaded /system/lib/egl/libEGL_mali.so
01-06 19:26:03.095: D/libEGL(25739): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-06 19:26:03.100: D/libEGL(25739): loaded /system/lib/egl/libGLESv2_mali.so
01-06 19:26:03.100: D/(25739): Device driver API match
01-06 19:26:03.100: D/(25739): Device driver API version: 10
01-06 19:26:03.100: D/(25739): User space API version: 10
01-06 19:26:03.100: D/(25739): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
01-06 19:26:03.135: D/OpenGLRenderer(25739): Enabling debug mode 0
01-06 19:26:05.680: D/AbsListView(25739): Get MotionRecognitionManager
01-06 19:26:09.415: D/dalvikvm(25739): GC_CONCURRENT freed 92K, 5% free 12473K/13127K, paused 22ms+7ms, total 67ms
01-06 19:26:09.425: D/AbsListView(25739): [unregisterDoubleTapMotionListener]
01-06 19:26:09.425: I/MotionRecognitionManager(25739): .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@42bfb580
01-06 19:26:12.515: D/dalvikvm(25739): GC_CONCURRENT freed 120K, 6% free 12813K/13575K, paused 3ms+2ms, total 21ms
01-06 19:26:12.515: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 14ms
01-06 19:26:13.665: D/dalvikvm(25739): GC_FOR_ALLOC freed 183K, 7% free 13024K/13895K, paused 17ms, total 18ms
01-06 19:26:15.205: D/dalvikvm(25739): GC_CONCURRENT freed 535K, 9% free 12937K/14151K, paused 2ms+3ms, total 19ms
01-06 19:26:15.205: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 16ms
01-06 19:26:17.095: D/dalvikvm(25739): GC_CONCURRENT freed 536K, 9% free 12904K/14151K, paused 4ms+2ms, total 25ms
01-06 19:26:17.095: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 7ms
01-06 19:26:17.160: D/dalvikvm(25739): GC_CONCURRENT freed 445K, 9% free 12893K/14151K, paused 12ms+12ms, total 35ms
01-06 19:26:19.765: D/dalvikvm(25739): GC_CONCURRENT freed 196K, 7% free 13201K/14151K, paused 5ms+5ms, total 36ms
01-06 19:26:19.765: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 13ms
01-06 19:26:23.260: D/dalvikvm(25739): GC_FOR_ALLOC freed 524K, 9% free 13025K/14279K, paused 18ms, total 18ms
01-06 19:26:26.350: D/dalvikvm(25739): GC_CONCURRENT freed 536K, 10% free 12992K/14279K, paused 3ms+2ms, total 22ms
01-06 19:26:26.350: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 5ms
01-06 19:26:28.290: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12948K/14279K, paused 6ms+2ms, total 26ms
01-06 19:26:28.290: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:26:29.445: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12904K/14279K, paused 7ms+2ms, total 30ms
01-06 19:26:29.445: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 7ms
01-06 19:26:29.510: D/dalvikvm(25739): GC_CONCURRENT freed 137K, 8% free 13201K/14279K, paused 3ms+13ms, total 36ms
01-06 19:26:32.065: D/dalvikvm(25739): GC_FOR_ALLOC freed 480K, 9% free 13068K/14279K, paused 17ms, total 17ms
01-06 19:26:36.005: D/dalvikvm(25739): GC_CONCURRENT freed 536K, 9% free 13036K/14279K, paused 14ms+4ms, total 49ms
01-06 19:26:36.005: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 12ms
01-06 19:26:38.665: D/dalvikvm(25739): GC_CONCURRENT freed 547K, 10% free 12992K/14279K, paused 15ms+4ms, total 48ms
01-06 19:26:38.665: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 15ms
01-06 19:26:40.605: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12948K/14279K, paused 13ms+2ms, total 32ms
01-06 19:26:40.605: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 6ms
01-06 19:26:43.160: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12904K/14279K, paused 13ms+2ms, total 34ms
01-06 19:26:43.160: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 4ms
01-06 19:26:43.225: D/dalvikvm(25739): GC_CONCURRENT freed 533K, 11% free 12805K/14279K, paused 2ms+13ms, total 32ms
01-06 19:26:46.360: D/dalvikvm(25739): GC_CONCURRENT freed 108K, 8% free 13201K/14279K, paused 3ms+2ms, total 22ms
01-06 19:26:46.360: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:26:47.980: D/dalvikvm(25739): GC_CONCURRENT freed 524K, 9% free 13080K/14279K, paused 12ms+2ms, total 34ms
01-06 19:26:47.980: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 12ms
01-06 19:26:50.405: D/dalvikvm(25739): GC_CONCURRENT freed 547K, 9% free 13036K/14279K, paused 2ms+3ms, total 21ms
01-06 19:26:50.405: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:26:53.925: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12992K/14279K, paused 4ms+4ms, total 38ms
01-06 19:26:53.925: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 18ms
01-06 19:26:55.260: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12948K/14279K, paused 8ms+2ms, total 36ms
01-06 19:26:55.260: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 10ms
01-06 19:26:57.080: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12904K/14279K, paused 22ms+2ms, total 46ms
01-06 19:26:57.080: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 10ms
01-06 19:26:57.135: D/dalvikvm(25739): GC_CONCURRENT freed 489K, 11% free 12849K/14279K, paused 2ms+12ms, total 29ms
01-06 19:26:58.420: D/dalvikvm(25739): GC_CONCURRENT freed 152K, 8% free 13201K/14279K, paused 13ms+2ms, total 33ms
01-06 19:26:58.420: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 6ms
01-06 19:26:59.565: D/dalvikvm(25739): GC_FOR_ALLOC freed 524K, 9% free 13025K/14279K, paused 18ms, total 19ms
01-06 19:27:02.100: D/dalvikvm(25739): GC_CONCURRENT freed 536K, 10% free 12992K/14279K, paused 2ms+2ms, total 22ms
01-06 19:27:02.100: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:27:03.685: D/dalvikvm(25739): GC_CONCURRENT freed 547K, 10% free 12948K/14279K, paused 7ms+3ms, total 29ms
01-06 19:27:03.685: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 8ms
01-06 19:27:06.535: D/dalvikvm(25739): GC_CONCURRENT freed 548K, 10% free 12904K/14279K, paused 18ms+3ms, total 52ms
01-06 19:27:06.535: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 9ms
01-06 19:27:06.580: D/dalvikvm(25739): GC_CONCURRENT freed 489K, 11% free 12849K/14279K, paused 2ms+2ms, total 18ms
01-06 19:27:08.065: D/dalvikvm(25739): GC_CONCURRENT freed 152K, 8% free 13201K/14279K, paused 3ms+1ms, total 41ms
01-06 19:27:08.065: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 19ms
01-06 19:27:11.150: D/dalvikvm(25739): GC_FOR_ALLOC freed 526K, 9% free 13025K/14279K, paused 17ms, total 17ms
01-06 19:27:12.600: D/dalvikvm(25739): GC_CONCURRENT freed 545K, 10% free 12982K/14279K, paused 4ms+5ms, total 52ms
01-06 19:27:12.600: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 26ms
01-06 19:27:15.065: D/dalvikvm(25739): GC_CONCURRENT freed 550K, 10% free 12938K/14279K, paused 12ms+3ms, total 37ms
01-06 19:27:15.065: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 6ms
01-06 19:27:15.125: D/dalvikvm(25739): GC_CONCURRENT freed 521K, 11% free 12805K/14279K, paused 3ms+12ms, total 32ms
01-06 19:27:19.575: D/dalvikvm(25739): GC_CONCURRENT freed 110K, 8% free 13201K/14279K, paused 12ms+3ms, total 41ms
01-06 19:27:19.575: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 4ms
01-06 19:27:29.260: D/dalvikvm(25739): GC_FOR_ALLOC freed 534K, 9% free 13028K/14279K, paused 15ms, total 19ms
01-06 19:27:50.315: D/dalvikvm(25739): GC_CONCURRENT freed 546K, 10% free 12957K/14279K, paused 13ms+2ms, total 39ms
01-06 19:27:50.315: D/dalvikvm(25739): WAIT_FOR_CONCURRENT_GC blocked 9ms
01-06 19:27:55.140: D/dalvikvm(25739): GC_CONCURRENT freed 537K, 11% free 12803K/14279K, paused 17ms+3ms, total 36ms
01-06 19:28:05.150: W/jdwp(25739): Debugger is telling the VM to exit with code=1
01-06 19:28:05.150: I/dalvikvm(25739): GC lifetime allocation: 42530 bytes
01-06 19:28:05.510: D/libEGL(27466): loaded /system/lib/egl/libEGL_mali.so
01-06 19:28:05.520: D/libEGL(27466): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-06 19:28:05.520: D/libEGL(27466): loaded /system/lib/egl/libGLESv2_mali.so
01-06 19:28:05.525: D/(27466): Device driver API match
01-06 19:28:05.525: D/(27466): Device driver API version: 10
01-06 19:28:05.525: D/(27466): User space API version: 10
01-06 19:28:05.525: D/(27466): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
01-06 19:28:05.550: D/OpenGLRenderer(27466): Enabling debug mode 0
01-06 19:28:19.100: D/libEGL(27760): loaded /system/lib/egl/libEGL_mali.so
01-06 19:28:19.105: D/libEGL(27760): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-06 19:28:19.110: D/libEGL(27760): loaded /system/lib/egl/libGLESv2_mali.so
01-06 19:28:19.110: D/(27760): Device driver API match
01-06 19:28:19.110: D/(27760): Device driver API version: 10
01-06 19:28:19.110: D/(27760): User space API version: 10
01-06 19:28:19.110: D/(27760): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
01-06 19:28:19.165: D/OpenGLRenderer(27760): Enabling debug mode 0
01-06 19:28:48.755: D/AbsListView(27760): Get MotionRecognitionManager
01-06 19:28:50.325: D/dalvikvm(27760): GC_CONCURRENT freed 107K, 6% free 12461K/13191K, paused 3ms+13ms, total 31ms
01-06 19:28:50.330: D/AbsListView(27760): [unregisterDoubleTapMotionListener]
01-06 19:28:50.330: I/MotionRecognitionManager(27760): .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@42bff9d8
01-06 19:28:55.810: D/dalvikvm(27760): GC_CONCURRENT freed 127K, 6% free 12813K/13575K, paused 3ms+3ms, total 22ms
01-06 19:28:55.810: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 16ms
01-06 19:28:56.915: D/dalvikvm(27760): GC_FOR_ALLOC freed 181K, 7% free 13024K/13895K, paused 18ms, total 18ms
01-06 19:28:58.235: D/dalvikvm(27760): GC_CONCURRENT freed 537K, 9% free 12936K/14151K, paused 2ms+2ms, total 14ms
01-06 19:28:58.235: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 10ms
01-06 19:28:59.130: D/dalvikvm(27760): GC_CONCURRENT freed 527K, 10% free 12793K/14151K, paused 2ms+2ms, total 13ms
01-06 19:29:00.920: D/dalvikvm(27760): GC_CONCURRENT freed 104K, 7% free 13200K/14151K, paused 3ms+3ms, total 31ms
01-06 19:29:00.920: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 13ms
01-06 19:29:06.065: D/dalvikvm(27760): GC_CONCURRENT freed 536K, 9% free 13068K/14279K, paused 5ms+4ms, total 36ms
01-06 19:29:06.065: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 30ms
01-06 19:29:16.625: D/dalvikvm(27760): GC_CONCURRENT freed 547K, 9% free 13027K/14279K, paused 14ms+4ms, total 50ms
01-06 19:29:16.625: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 16ms
01-06 19:29:36.525: D/dalvikvm(27760): GC_CONCURRENT freed 540K, 10% free 12988K/14279K, paused 15ms+4ms, total 56ms
01-06 19:29:36.525: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 21ms
01-06 19:30:06.085: W/IInputConnectionWrapper(27760): showStatusIcon on inactive InputConnection
01-06 19:30:13.920: D/dalvikvm(27760): GC_CONCURRENT freed 549K, 10% free 12926K/14279K, paused 20ms+8ms, total 100ms
01-06 19:30:13.920: D/dalvikvm(27760): WAIT_FOR_CONCURRENT_GC blocked 50ms
01-06 19:30:13.995: D/dalvikvm(27760): GC_CONCURRENT freed 137K, 8% free 13223K/14279K, paused 13ms+3ms, total 44ms
答案 0 :(得分:6)
AsyncTask中存在巨大的愚蠢。引用开发文档:
“首次引入时,AsyncTasks在一个后台线程上串行执行。从DONUT开始,这被改为一个线程池,允许多个任务并行运行。从HONEYCOMB开始,任务在一个线程上执行避免因并行执行而导致的常见应用程序错误。
“如果你真的想要并行执行,你可以用THREAD_POOL_EXECUTOR调用executeOnExecutor(java.util.concurrent.Executor,Object [])。”
这意味着在AsyncTask :: doInBackground中休眠将阻止所有其他AsyncTasks运行,直到睡眠完成。您可能正在阻止其他后台活动(可能是系统库活动)完成。
答案 1 :(得分:1)
如果您希望在不久的将来做某事,可以使用handler或timer。
示例:
这将在大约2秒内运行runnable:
new Handler().postDelayed(new Runnable() {...} , 2000);
asyncTask仅用于小任务,您不关心它们完成时的顺序,也不关心处理它们的线程数。
例如,它们可用于从互联网上下载小图像,如果它们被用于滚动速度过快的列表视图,则会被取消。
无论如何,我们需要看一些你的代码:什么是“setCanPlay”?和其他asyncTask的调用在哪里?
答案 2 :(得分:0)
我发现问题出在那=> AsyncTask类使用串行通道在新版本的android上编译您的应用,例如软糖== 4.2
不同于旧版本,例如Eclair版本=> 2.0 它使用并行通道来编译AsyncTask,因此,如果您在旧版本上尝试您的应用 就像我做过的一样,您会看到与众不同 You See the Different Here
如果您想解决此问题,将为您提供帮助:-
调用executeOnExecutor(java.util.concurrent.Executor, Object[])