Forum Xamarin.iOS


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

CPU Usage

DeanCleaverDeanCleaver USMember ✭✭✭

I've had problems since version 5.1.3 with battery usage, even when my app is idle. I've just done a test with a blank app and my app, but have some interesting results.

If I run the blank app, and then use instruments to monitor Activity I can see it uses a few fractions of MS of CPU time every 10 seconds or so.

If I run up my real app, it shows it's using much more CPU time. If I pause the debugger, it shows no threads active except the Main thread, and Instruments continues to show CPU usage for my app.

Any ideas what this CPU usage could be, and could it be the cause of the battery drain? Basically, if I leave my app open at the login screen doing nothing with only a 3G connection it will die in 6 hours from full battery and I just don't know why as it's doing absolutely nothing. Wifi it lasts about twice as long, and obviously without my app running way way longer.

Any ideas on how I could find out what is ticking over and why?


  • RolfBjarneKvingeRolfBjarneKvinge USXamarin Team Xamurai

    You should be able to use Time Profiler in Instruments to find out what's using the CPU.

  • DeanCleaverDeanCleaver USMember ✭✭✭

    I've tried a lot of them but without much success. I did leave my app running for about 20 hours, and in that time it consumed almost 1 hour of CPU time doing absolutely nothing.

    I'll try the time profiler again to see if I can get more information, but I've not had a huge amount of success profiling anything.

  • DeanCleaverDeanCleaver USMember ✭✭✭

    Rolf, after a lot of mucking around I've managed to reduce my idle CPU usage by a factor of more than 130 times!

    First culprit was "UIDevice.CurrentDevice.BatteryMonitoringEnabled". It seems to chew battery if turned on - but it didn't seem to in 5.2.12 and prior - do you know if anything changed in this area?

    Next culprit was DateTime.Now - seems that's a relatively expensive call. I was rather stupidly using it in loops etc and after making a local copy and using that in the loops, I cut the CPU use quite a lot.

    Next was to change all System.Timers.Timer to System.Threading.Timer - the former calls DateTime.Now on every event (and I had 1 at least every 15 seconds). Over a long time of use that adds up.

    Now, the highest CPU use while my app is logged in but idle seems to be in ServicePoint.AvailableForRecycling... which calls DateTime.Now.

    Is it nit-picking, or would it be possible to get the idleSince variable changed to use DateTime.UtcNow which seems to be more efficient, and then use local/utc conversions in the public property? Would mean no change to functionality, but seems to me like it would be more efficient - especially as this seems to get called on behalf of my app every 15 seconds. And my app is used 24/7 in some locations, so it adds up to a lot over a day.

  • RolfBjarneKvingeRolfBjarneKvinge USXamarin Team Xamurai

    This is actually very good feedback!

    Can you file enhancement request in bugzilla with the issues you find? Otherwise I'm afraid it can get lost in the forums, it's a lot easier to keep track with bugzilla.

  • DeanCleaverDeanCleaver USMember ✭✭✭

    I have updated my original battery life bug with the UIDevice.CurrentDevice.BatteryMonitoringEnabled information, and created another bug regarding the switch to DateTime.UtcNow.

Sign In or Register to comment.