Timer object initiating function over extended period of time using too much CPU memory
7 views (last 30 days)
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
Answers (2)
Walter Roberson
on 4 Sep 2012
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!