Timer object initiating function over extended period of time using too much CPU memory
Show older comments
I am attempting to run a timer object that refers to a function that reads and plots a data point from a pH probe. I plan on running this timer object for up to 3 days, but after 2-3 hours matlab and the computer crashes because CPU usage slowly increases to 100%.
To start the timer I'm using:
h.measuretimer = timer
set(h.measuretimer, 'executionMode', 'fixedRate', 'Period', 4, 'TimerFcn', 'Cori_read')
start(h.measuretimer)
My attempts at fixing this problem have been: 1. Using the "return" function at the end of the code being called upon by the timer object.
Result: CPU crashed after running for 2-3 hours
2. clearing and deleting the timer object after each single measurement, then restarting it.
To do this I wrote: stop(h.measuretimer) delete(h.measuretimer) clear h.measuretimer clearvars return
Result: CPU crashed after running for 2-3 hours
3. Removing the timer object all together and replace with a while loop.
Result: Program ran for 3 days with no problems, CPU usage stayed below 1%. However, this solution prevents further applications I hope to use with this program because the measurements are interrupted when I perform other actions.
My questions are: 1) Why is this happening? 2) Is there a way to properly end a script and remove all memory it used? 3) How do you properly use a timer object over an extended period of time?
Thank you
9 Comments
How long does it take Cori_read to execute? Is Cori_read a script or a function? Why are you using the string instead of @Cori_read?
And where is the code you show above? Hopefully not in Cori_read! I am trying to make sure you are not creating many timers inadvertently.
Sean
on 4 Sep 2012
Cori_read may take less than 4 seconds, but when using fixedRate you are also counting the queue lag and any other processes. Please give this TimerFcn a try and see if anything suspicious happens....
'disp(''In''),Cori_read,disp(''Out'')'
By the third question, I am asking why you are using a callback string instead of a function handle.
Sean
on 4 Sep 2012
Jan
on 4 Sep 2012
What exactly does "crash" mean? The CPU usage is going slowly to 100%, but this is not a crash. Do you get an error message or core dump?
I assume there is either a missing pre-allocation in the Cori_read function, or you append to a file, which is opened and closed repeatedly. You can use the profiler to see, which commands need more and more time.
Sean
on 4 Sep 2012
Sean de Wolski
on 4 Sep 2012
If we could have the code for Cori_read that would be ideal. But yes, otherwise please try a simple timerfcn and see if the CPU still eventually goes to 100.
Sean
on 4 Sep 2012
Sean
on 4 Sep 2012
Answers (2)
Walter Roberson
on 4 Sep 2012
0 votes
Don't use plot() each time through the timer callback. Instead, create a line object the first time through, and after that set() the XData and YData of that line object to update it.
Do you really need to plot all 100,000 points? The graphics effort for that is going to be non-trivial and get worse as you gather more points. At 100,000 points your resolution will be what, 100 points per pixel? Consider plotting only every h.number/1000 pixel (assuming a plot window 1000 pixels wide).
Also, as you go you are having to change the x-axis limits each time because you are plotting more and more time. Consider picking a time-window, last N seconds only, and displaying that, with xlimmode set to manual and you adjusting the xlim from time to time but not every update.
1 Comment
Malcolm Lidierth
on 9 Sep 2012
You could plot then just update the YData values - much faster than a high level plot.
Opening/closing the file each time could be avoided by passing a file handle to the callback and closing it on timer deletion.
Also don't use clock - it's not reliable because the OS may reset system time during the 3 days.
As @Sean asked, does this happen with a trivial (e.g. empty) callback?
Daniel Shub
on 5 Sep 2012
If h.number ever exceeds 100000, and it looks like it might since you do the following check
if h.number<99950
Then you phlog and timelog are not preallocated and growing on every timer callback. This could take a lot of time. Further, plotting 100000 points is a lot. Even if each point is 1x1 pixels, you would be using 20% of your monitor.
Categories
Find more on App Building in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!