Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Clearing TIMER objects

Subject: Clearing TIMER objects

From: Jan Simon

Date: 23 Oct, 2010 21:19:05

Message: 1 of 4

Dear readers,

I'm looking for a clean way to get rid of a timer object.

I'm starting a timer inside a function, to delete a figure automatically after a certian delay. But how should I clear the timer object afterwards?

--------------------------- 8< ---------------
  FigH = figure;
  timer(TimerFcn', {@CloseTimer, FigH}, ...
     'ExecutionMode', 'singleShot', ...
     'StartDelay', 300);

  function CloseTimer(TimerH, EventData, FigH)
  if ishandle(FigH) % Not closed manually before
    delete(FigH)
  end
  stop(TimerH); % ???
  delete(TimerH); % ???
  clear(TimerH); % ???
------------------------------- >8 ------------
Is it safe to STOP and DELETE the timer from within its own callback?
It works in Matlab 6.5 and 2009a. But an equivalent approach with an AUDIOPLAYER object let Matlab 2009a crash.

As far as I can see, singleShot TIMER objects are a memory leak, if they do not delete themselves. Or is there a garbage collector for ophaned TIMERs?
Can somebody please tell me to concerning page in the documentation?

Thanks in advance, Jan

Subject: Clearing TIMER objects

From: Bruno Luong

Date: 24 Oct, 2010 08:51:03

Message: 2 of 4

Jan,

What I write below is what I think logically thing should be, not writing in official document.
You can't delete the timer within the callback. You can however stop and start it.

The place where you should delete it is logically at the same level where you create the timer, i.e., must be outside the callback.

There are two strategies of launching a timer:
1. blocking, i.e., wait for the timer who finish the task, then do something else
2. non blocking, launch the timer then do something else.

Both will have different programming flow

Blocking:

functio blockingprocessing

TIMERHANDLE = timer('Period', 5, ... % check every 5 seconds
                    'Name', 'MyTimer', ...
                    'TimerFcn', @mycallback, ...
                    'TasksToExecute', intmax());
start(TIMERHANDLE);
wait(TIMERHANDLE);
delete(TIMERHANDLE);

%%%%%%%%%%%%%%%%%%%%%%

Non blocking: you create the timer once, then start and stop it when needed.

function nonblockingprocessing

TIMERHANDLE = timerfind('Name', 'MyTimer');
if isempty(TIMERHANDLE)
    TIMERHANDLE = timer('Period', 5, ... % check every 5 seconds
        'Name', 'MyTimer', ...
        'TimerFcn', @mycallback, ...
        'TasksToExecute', intmax());
end
start(TIMERHANDLE);

You could delete it when you no longer need timer, but do it outside the callback. When to do it is up to you.

The "clear" instruction is no more than clearing a variable, that anyway happens when the function returns. You don't have to do that.

Bruno

Subject: Clearing TIMER objects

From: Jan Simon

Date: 24 Oct, 2010 13:12:03

Message: 3 of 4

Dear Bruno,

thanks for your helpful answer.

> The "clear" instruction is no more than clearing a variable, that anyway happens when the function returns. You don't have to do that.

This is my *opinion* also. But look at this example:
  function A
    TimerH = timer('TimerFcn', @StopMe, 'ExecutionMode', 'singleShot', ...
      'StartDelay', 10);
  return;
Now Matlab's friendly memory management has cleared [TimerH] automatically, but the timer object is still existing anywhere in the memory (where? In the Java heap space?). As far as I understand, DELETE is the correct way to remove it - it calls the destructor of the object. For the builtin types, CLEAR destructs the objects also and releases the memory.

Anyhow, I stop to delete the timer from inside its callback and follow the clearer way to delete it externally, for the cost of some stopped timer objects hanging around in the memory and waiting for their garbage collection. The task I'm working on is a non-blocking self-terminating message window. The TIMER deletes the figure after a delay.
Now myMsgBox function calls TIMERFIND to cleanup formerly created but stopped timers and deletes them. As drawback one or more stopped TIMERs can sleep in the memory and wait for Matlab's shut down or the next call of myMsgBox.

The documentation should explain explicitely how TIMERs are cleared securely from the memory - e.g. if somebody creates 10^6 TIMERs... I'll send an enhancement request.

Thanks again, Jan

Subject: Clearing TIMER objects

From: Bruno Luong

Date: 24 Oct, 2010 14:30:07

Message: 4 of 4

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <ia1bb3$601$1@fred.mathworks.com>...
> Dear Bruno,
>
> thanks for your helpful answer.
>
> > The "clear" instruction is no more than clearing a variable, that anyway happens when the function returns. You don't have to do that.
>
> This is my *opinion* also. But look at this example:
> function A
> TimerH = timer('TimerFcn', @StopMe, 'ExecutionMode', 'singleShot', ...
> 'StartDelay', 10);
> return;
> Now Matlab's friendly memory management has cleared [TimerH] automatically, but the timer object is still existing anywhere in the memory (where? In the Java heap space?). As far as I understand, DELETE is the correct way to remove it - it calls the destructor of the object. For the builtin types, CLEAR destructs the objects also and releases the memory.
>

It is not hard to make the inference that timer objects are not released after the function exits, since the timer is still alive. The behavior is just like a handle of graphic objects: we can create a FIGURE within a function, when we quits a function (where figure was created) the FIGURE is still there until CLOSE or DELETE on the handle is called. Where it resides? Probably the same place where all handle objects reside.

> Anyhow, I stop to delete the timer from inside its callback and follow the clearer way to delete it externally, for the cost of some stopped timer objects hanging around in the memory and waiting for their garbage collection. The task I'm working on is a non-blocking self-terminating message window. The TIMER deletes the figure after a delay.
> Now myMsgBox function calls TIMERFIND to cleanup formerly created but stopped timers and deletes them. As drawback one or more stopped TIMERs can sleep in the memory and wait for Matlab's shut down or the next call of myMsgBox.
>
> The documentation should explain explicitely how TIMERs are cleared securely from the memory - e.g. if somebody creates 10^6 TIMERs... I'll send an enhancement request.
>
> Thanks again, Jan

The concept of timers and in more general multi-threading programming is like that. It is no longer easy too structure the constructor and destructor in a neat and simple way. You might need to program a mater timer that take care of creating and deleting the timers which really do the work under the hood. What is lacking in Matlab is a multi-threading capability and a mechanism of sending/receiving events between threads/processes. This is an entire new territory. We shouldn't expect too much with the limitation of the current base.

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us