Android : Unexpected behaviour when running Service

on Monday, March 23, 2015


In my app I have a service. It is started when the main activity starts and does the following:



timer.scheduleAtFixedRate (
new TimerTask() {
public void run() {
appendToFile("diff: "+(System.currentTimeMillis()-lastTime));
lastTime = System.currentTimeMillis();
}
}, 0, 1);


Basically, it creates a Timer which updates every millisecond. On each update the timer looks at when it was last called, and saves the time between timer calls to a file. As there is always overhead in the system I expect each line of the output to be more than 1ms, maybe around 10. Here is a snapchat of the app when it is asleep.



diff: 6
diff: 8
diff: 5
diff: 81
diff: 12
diff: 12
diff: 54
diff: 60
diff: 5734
diff: 77
diff: 9
diff: 20
diff: 6
diff: 21
diff: 10
diff: 13
diff: 16895
diff: 13
diff: 82
diff: 269
diff: 35
diff: 25
diff: 24
diff: 20
diff: 30
diff: 26
diff: 11
diff: 14
diff: 49
diff: 57
diff: 9


My question is regarding the somewhat weird results I sometimes get. 16895 in the middle indicates that the service for some reason waited almost 17 seconds to run its task when the system was idle. However, directly afterwards it shows more normal results between 10 and 30 milliseconds. I know that this is during the time the system was sleeping, because the log has a lot more entries. The service was never destroyed, as I log all calls to onDestroy() as well.


Is there an explanation why the service would suddenly pause? Is there a way that I can ensure that it does run all the time?


0 comments:

Post a Comment