private void optimizeTasksLocked(){
  ArrayList<ActivityManager.RecentTaskInfo> recentTask=new ArrayList<>(mAM.getRecentTasks(Integer.MAX_VALUE,ActivityManager.RECENT_WITH_EXCLUDED | ActivityManager.RECENT_IGNORE_UNAVAILABLE));
  int N=mHistory.size();
  while (N-- > 0) {
    TaskRecord task=mHistory.valueAt(N);
    ListIterator<ActivityManager.RecentTaskInfo> iterator=recentTask.listIterator();
    boolean taskAlive=false;
    while (iterator.hasNext()) {
      ActivityManager.RecentTaskInfo info=iterator.next();
      if (info.id == task.taskId) {
        taskAlive=true;
        iterator.remove();
        break;
      }
    }
    if (!taskAlive) {
      mHistory.removeAt(N);
    }
  }
}

private void optimizeTasksLocked ( ) { ArrayList < ActivityManager . RecentTaskInfo > recentTask = new ArrayList < > ( mAM . getRecentTasks ( Integer . MAX_VALUE , ActivityManager . RECENT_WITH_EXCLUDED | ActivityManager . RECENT_IGNORE_UNAVAILABLE ) ) ; int N = mHistory . size ( ) ; while ( N -- > 0 ) { TaskRecord task = mHistory . valueAt ( N ) ; ListIterator < ActivityManager . RecentTaskInfo > iterator = recentTask . listIterator ( ) ; boolean taskAlive = false ; while ( iterator . hasNext ( ) ) { ActivityManager . RecentTaskInfo info = iterator . next ( ) ; if ( info . id == task . taskId ) { taskAlive = true ; iterator . remove ( ) ; break ; } } if ( ! taskAlive ) { mHistory . removeAt ( N ) ; } } }
app started in va may be removed in overview screen , then ams . removetask will be invoked , all data struct about the task in ams are released , while the client ' s process is still alive . so remove related data in va as well . a new taskrecord will be recreated in ` onactivitycreated `
