0

So, I have one problem, which I can't solve. My app used over 45% of cpu samsung SII. I think the main reason is postDelayed. Here is part of my code:

           Handler a=new Handler(); 
           a.post(new Runnable(){

        @Override
        public void run() {    
        Calendar cal = Calendar.getInstance(Locale.US); 
        cal.setTime(curr); 
        Format formatter = new SimpleDateFormat("\r EE \n d");
        int currentDOW = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_YEAR,(currentDOW*-1)+i+2);
        den.setText(formatter.format(cal.getTime()));  
        }
            a.postDelayed(this,400); 
        });

So, this is part of my code, it is work, but I think, it is the main reason of high CPU usage. Please help me! Thank you!

AShelly
  • 34,686
  • 15
  • 91
  • 152
yota9
  • 37
  • 1
  • 3
  • 8

2 Answers2

0

you could optimize the code a bit , but i suspect that you simply don't stop the updating when you don't need to , so more and more updates are being accumulated .

as an example , if you change the orientation , it will add more events of the previous activitiy (which was destroyed) and you end up with double the rate of updating .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • can be. Can you please give code how to stop it? I tried onstop, but it wasn't work (or I done it with error). Thank you! – yota9 Aug 11 '12 at 13:12
  • just take the handler and the runnable , and call : removeCallbacks () , as shown in the API : http://developer.android.com/reference/android/os/Handler.html#removeCallbacks%28java.lang.Runnable%29 – android developer Aug 11 '12 at 13:15
  • btw, about optimizing the code , i'm not sure what you've written , but you could put some of the constant variables outside and call as few time as possible to "new" . for example "cal" and "formatter" could be easily be outside of the runnable and be initiated just once . – android developer Aug 11 '12 at 13:17
  • I done a.removeCallbacks(this); but it doesn't work. Yes CPU usage is on 0%, but it doesn't refresh GUI now... – yota9 Aug 11 '12 at 14:49
  • you should do it when you want to stop/pause the refreshing . when you wish to resume the refreshing , run the runnable again . btw, you don't have to call the "a.post" in the beginning . just run the runnable instead. – android developer Aug 11 '12 at 15:36
  • without "a.post" many mistakes. So when my program is working, it will use 50% of cpu, is it normal? – yota9 Aug 11 '12 at 20:50
  • I made small mistake in code. Now CPU usage is nearly 5%. But I think It is high too. ANd if I will removecallbacks while I pause \ stop app it will be ok, but when I will run it it will be 5%, I dont think it is good. Maybe 1% will be good, but not 5 – yota9 Aug 11 '12 at 21:48
  • well , according to the code i see , you need to update the UI only when a day has passed , so why not check if the value of the current day is different than the previous one ? better yet, why update it every 400 ms instead of the real time between now and the new day that comes? – android developer Aug 11 '12 at 21:55
  • you could even use TimerTask instead , though i've never used it before. – android developer Aug 11 '12 at 21:57
0

I had this problem. My app was using around 60% CPU time until I added the following code to my run() method in my worker thread:

@Override
public void run()
{
    while( _running )
    {
        // do usual stuff
        // ...

        // ** add the following to the end of your loop **

        try
        {
            Thread.sleep( 5 );
        }
        catch( InterruptedException e )
        {
            _running = false;
            /* at least we tried */
        }
    }
}

It now uses ~8% CPU with no noticeable difference.

Experiment with the value '5'. I've added an app setting to allow the user to balance better graphics (i.e. number lower than 5) with better battery usage (number higher than 5).

William Morrison
  • 10,953
  • 2
  • 31
  • 48
wh1974
  • 11
  • 2