interpreting traceview output

Put your problem here if it does not fit any of the other categories.

interpreting traceview output

Postby rackham » Thu Dec 03, 2009 7:33 pm

Hello!

If anyone can help me with this problem id be very much appreciative!

In my app im writing I have started getting the dreaded ANR. Im pretty sure of why its being caused, I have a method which can take more than 5 seconds to process (just processes a while bunch of string data). I have this running in it's own thread so the UI dosent get blocked whilst this is happening, when finished it calls a method from a handler object that's being created from the UI thread, which then updates the UI appropraitly. This isnt behaving as expected as it is still causing the UI input to block. Looking at my traceview output i can see where this processing takes place (Thread-43) but it seems that its actually blocking the other threads (including the main thread) from running, as you can see that any method call data representation is missing for the entire period the string sorting thread is running.

I wanted to ask, am i interpreting the traceview correctly? if so what am i doing wrong here?

the offending code below, any help would be great, thanks :)


Code: Select all
@Override
   public void listReceived(final ArrayList<String> list) {
      Debug.startMethodTracing("processList");
      //long running task (up to 10 secs) so have new thread
      final Handler handler = new Handler(){
         public void handleMessage(Message msg) {
            if(msg.what == Global.TASK_FINSIHED){
               //clear the main list, then copy over the temp new list
               clear();
               addAll(userDataTemp);
               sortByField(FIELD_NAME); //short process
               UsersData.this.informListenersDataChanged(DataChangedListener.ALL_USER_DATA);            
               Debug.stopMethodTracing();
            }
         }
      };
                  
      Thread processList = new Thread(){
         ArrayList<String> listCopy = (ArrayList<String>)list.clone();
         
         @Override
         public void run(){
            Log.d(TAG, "processing list");
            Log.d(TAG, "nonUIThread:"+Thread.currentThread().getId());
            UsersData.this.processRawList(listCopy);
            handler.sendMessage(handler.obtainMessage(Global.TASK_FINSIHED));
         }
      };
      Log.d(TAG, "UIThread:"+Thread.currentThread().getId());
      processList.start();            
   }
rackham
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Oct 06, 2009 10:47 am

Top

Postby appforce » Thu Dec 03, 2009 8:18 pm

Hi,

It seems to me that you have a heavy operation in handleMessage of you Handler - that is executed on the main thread. It's probably addAll.


Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby rackham » Thu Dec 03, 2009 11:09 pm

hmm yes, it seems your right, it's only copyinh a couple of hundred String arrays (of length 6), each with only a couple of charecters in, but it does seem to take a perfomance hit, moving it out into another thread cures the problem.

a related question. When my other threads start running, the emulator become a little jerky, so when scrolling a listview. Any tips on how to cure this (everything seems a little show in the emu anyway, so maybe will be fine on a device), setting a lower thread prority or letting it sleep a little? Any tips would be great!

Thanks for the help

Dori
rackham
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Oct 06, 2009 10:47 am

Postby appforce » Fri Dec 04, 2009 10:53 am

Hi Dori,

Generally the emulator is slower than device, so it's not too useful for judging performance. It's good to put some sleep, count every X strings copied and put few milliseconds sleep or yield. Priority is behaving different on various VMs and I haven't done much testing on Dalvik to tell if it's usable for this.


Android developers
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby appforce » Fri Dec 04, 2009 10:54 am

One more thing - why copy so many String arrays? Can't this be done without copying?
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 16 guests