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:
OO matlab - deleting objects

Subject: OO matlab - deleting objects

From: krindikzulmann@gmail.com

Date: 18 May, 2009 07:19:11

Message: 1 of 7

Do we have to do explicit memory cleanup in matlab

eg.


%%%%%%%%%%%%%%%%
classdef ContainerModule < Module
methods
function obj = ContainerModule(Fs)

obj.bpfhandle = OSCModule();
end

function delete(obj)

% here
delete(obj.bpfhandle);

end

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



when the object is destroyed isn't the member object delete() method
automatically called?


thanks

Subject: OO matlab - deleting objects

From: Nasser Abbasi

Date: 18 May, 2009 08:43:50

Message: 2 of 7


<krindikzulmann@gmail.com> wrote in message
news:0beda853-2849-4199-9986-2543f502f482@d7g2000prl.googlegroups.com...
> Do we have to do explicit memory cleanup in matlab
>

No. See below

http://www.mathworks.com/company/newsletters/digest/2008/sept/matlab-objects.html

"Object Life-Cycle Management
MATLAB implements a strategy for object life-cycle management that supports
destructors and destroys objects as soon as they become unreachable from any
MATLAB workspace. The MATLAB language has always managed memory allocation
by destroying workspace variables when a function exits, either through an
error or through a normal return to the calling context. We wanted to
preserve this simple model but also extend it to handle objects that might
persist beyond the execution of a function (by being returned to a calling
function or stored in some other object returned to a calling function).
A MATLAB handle class can define a delete method that behaves very much like
a destructor in languages like C++ (Figures 3a and 3b). In MATLAB, a delete
method is called just before an object is destroyed because the object can
no longer be accessed from any MATLAB variable. The delete method can be
used to close a file, close an external application, or notify another
object that needs to react to the destruction of the first object. It is
defined by the handle class, and only handle classes have destructors in
MATLAB. "

"Some programmers might find it strange that non-handle objects have no
destructors. Since MATLAB object destruction is always automated, a
non-handle object that holds other objects does not need to do anything to
destroy those other objects."


--Nasser

Subject: OO matlab - deleting objects

From: krindikzulmann@gmail.com

Date: 19 May, 2009 00:36:36

Message: 3 of 7

On May 18, 6:43 pm, "Nasser Abbasi" <n...@12000.org> wrote:
> <krindikzulm...@gmail.com> wrote in message
>
> news:0beda853-2849-4199-9986-2543f502f482@d7g2000prl.googlegroups.com...
>
> > Do we have to do explicit memory cleanup in matlab
>
> No. See below
>
> http://www.mathworks.com/company/newsletters/digest/2008/sept/matlab-...
>
> "Object Life-Cycle Management
> MATLAB implements a strategy for object life-cycle management that suppor=
ts
> destructors and destroys objects as soon as they become unreachable from =
any
> MATLAB workspace. The MATLAB language has always managed memory allocatio=
n
> by destroying workspace variables when a function exits, either through a=
n
> error or through a normal return to the calling context. We wanted to
> preserve this simple model but also extend it to handle objects that migh=
t
> persist beyond the execution of a function (by being returned to a callin=
g
> function or stored in some other object returned to a calling function).
> A MATLAB handle class can define a delete method that behaves very much l=
ike
> a destructor in languages like C++ (Figures 3a and 3b). In MATLAB, a dele=
te
> method is called just before an object is destroyed because the object ca=
n
> no longer be accessed from any MATLAB variable. The delete method can be
> used to close a file, close an external application, or notify another
> object that needs to react to the destruction of the first object. It is
> defined by the handle class, and only handle classes have destructors in
> MATLAB. "
>
> "Some programmers might find it strange that non-handle objects have no
> destructors. Since MATLAB object destruction is always automated, a
> non-handle object that holds other objects does not need to do anything t=
o
> destroy those other objects."
>
> --Nasser


Thanks. But I saw the above mentioned code from an experienced matlab
pro

Even mathworks says below
"a non-handle object that holds other objects does not need to do
anything to
destroy those other objects"

does this imply handle objects do have to clean objects inside them
explicitly?

Subject: OO matlab - deleting objects

From: Steven Lord

Date: 19 May, 2009 13:46:29

Message: 4 of 7


<krindikzulmann@gmail.com> wrote in message
news:bdda5d36-2ea2-4cc9-b8c3-efb23421e304@y34g2000prb.googlegroups.com...
> On May 18, 6:43 pm, "Nasser Abbasi" <n...@12000.org> wrote:
> > <krindikzulm...@gmail.com> wrote in message
> >
> > news:0beda853-2849-4199-9986-2543f502f482@d7g2000prl.googlegroups.com...
> >
> > > Do we have to do explicit memory cleanup in matlab
> >
> > No. See below
> >
> > http://www.mathworks.com/company/newsletters/digest/2008/sept/matlab-...
> >
> > "Object Life-Cycle Management
> > MATLAB implements a strategy for object life-cycle management that
> > supports
> > destructors and destroys objects as soon as they become unreachable from
> > any
> > MATLAB workspace. The MATLAB language has always managed memory
> > allocation
> > by destroying workspace variables when a function exits, either through
> > an
> > error or through a normal return to the calling context. We wanted to
> > preserve this simple model but also extend it to handle objects that
> > might
> > persist beyond the execution of a function (by being returned to a
> > calling
> > function or stored in some other object returned to a calling function).
> > A MATLAB handle class can define a delete method that behaves very much
> > like
> > a destructor in languages like C++ (Figures 3a and 3b). In MATLAB, a
> > delete
> > method is called just before an object is destroyed because the object
> > can
> > no longer be accessed from any MATLAB variable. The delete method can be
> > used to close a file, close an external application, or notify another
> > object that needs to react to the destruction of the first object. It is
> > defined by the handle class, and only handle classes have destructors in
> > MATLAB. "
> >
> > "Some programmers might find it strange that non-handle objects have no
> > destructors. Since MATLAB object destruction is always automated, a
> > non-handle object that holds other objects does not need to do anything
> > to
> > destroy those other objects."
> >
> > --Nasser
>
>
> Thanks. But I saw the above mentioned code from an experienced matlab
> pro
>
> Even mathworks says below
> "a non-handle object that holds other objects does not need to do
> anything to
> destroy those other objects"
>
> does this imply handle objects do have to clean objects inside them
> explicitly?

No.

When a non-handle object is no longer accessible, it and all its properties
are cleared from memory just like normal variables when they go out of scope
(like when the function whose workspace they are in exits.) If that
non-handle object contains another object in one of its properties, then if
that other object is a non-handle object it too will be cleared from memory;
if it is a handle object, its delete method (if any) will be called, then it
will be cleared from memory. Thus in neither of those situations do you,
the user, need to do anything special.

When a handle object is no longer accessible, the same procedure as I
described above happens, except that its delete method is called before it
is cleared from memory. The reason the text above calls out non-handle
objects, I believe, is because they operate somewhat differently than users
may expect based on their experience with other object-oriented languages
(in that they don't have destructors.)

--
Steve Lord
slord@mathworks.com

Subject: OO matlab - deleting objects

From: krindikzulmann@gmail.com

Date: 19 May, 2009 23:02:05

Message: 5 of 7

On May 19, 11:46 pm, "Steven Lord" <sl...@mathworks.com> wrote:
> <krindikzulm...@gmail.com> wrote in message
>
> news:bdda5d36-2ea2-4cc9-b8c3-efb23421e304@y34g2000prb.googlegroups.com...
>
>
>
>
>
> > On May 18, 6:43 pm, "Nasser Abbasi" <n...@12000.org> wrote:
> > > <krindikzulm...@gmail.com> wrote in message
>
> > >news:0beda853-2849-4199-9986-2543f502f482@d7g2000prl.googlegroups.com.=
..
>
> > > > Do we have to do explicit memory cleanup in matlab
>
> > > No. See below
>
> > >http://www.mathworks.com/company/newsletters/digest/2008/sept/matlab-.=
..
>
> > > "Object Life-Cycle Management
> > > MATLAB implements a strategy for object life-cycle management that
> > > supports
> > > destructors and destroys objects as soon as they become unreachable f=
rom
> > > any
> > > MATLAB workspace. The MATLAB language has always managed memory
> > > allocation
> > > by destroying workspace variables when a function exits, either throu=
gh
> > > an
> > > error or through a normal return to the calling context. We wanted to
> > > preserve this simple model but also extend it to handle objects that
> > > might
> > > persist beyond the execution of a function (by being returned to a
> > > calling
> > > function or stored in some other object returned to a calling functio=
n).
> > > A MATLAB handle class can define a delete method that behaves very mu=
ch
> > > like
> > > a destructor in languages like C++ (Figures 3a and 3b). In MATLAB, a
> > > delete
> > > method is called just before an object is destroyed because the objec=
t
> > > can
> > > no longer be accessed from any MATLAB variable. The delete method can=
 be
> > > used to close a file, close an external application, or notify anothe=
r
> > > object that needs to react to the destruction of the first object. It=
 is
> > > defined by the handle class, and only handle classes have destructors=
 in
> > > MATLAB. "
>
> > > "Some programmers might find it strange that non-handle objects have =
no
> > > destructors. Since MATLAB object destruction is always automated, a
> > > non-handle object that holds other objects does not need to do anythi=
ng
> > > to
> > > destroy those other objects."
>
> > > --Nasser
>
> > Thanks. But I saw the above mentioned code from an experienced matlab
> > pro
>
> > Even mathworks says below
> > "a non-handle object that holds other objects does not need to do
> > anything to
> > destroy those other objects"
>
> > does this imply handle objects do have to clean objects inside them
> > explicitly?
>
> No.
>
> When a non-handle object is no longer accessible, it and all its properti=
es
> are cleared from memory just like normal variables when they go out of sc=
ope
> (like when the function whose workspace they are in exits.)  If that
> non-handle object contains another object in one of its properties, then =
if
> that other object is a non-handle object it too will be cleared from memo=
ry;
> if it is a handle object, its delete method (if any) will be called, then=
 it
> will be cleared from memory.  Thus in neither of those situations do yo=
u,
> the user, need to do anything special.
>
> When a handle object is no longer accessible, the same procedure as I
> described above happens, except that its delete method is called before i=
t
> is cleared from memory.  The reason the text above calls out non-handle
> objects, I believe, is because they operate somewhat differently than use=
rs
> may expect based on their experience with other object-oriented languages
> (in that they don't have destructors.)
>
> --
> Steve Lord
> sl...@mathworks.com

thanks.

So i believe the bottom line is
when a handle is destroyed if it has handle members they too are
automatically deleted(delete() called if it exists)
may be good news for lazy c++ programmers :)

Subject: OO matlab - deleting objects

From: Andrew Groth

Date: 27 Feb, 2013 20:49:08

Message: 6 of 7

> No.
>
> When a non-handle object is no longer accessible, it and all its properties
> are cleared from memory just like normal variables when they go out of scope
> (like when the function whose workspace they are in exits.) If that
> non-handle object contains another object in one of its properties, then if
> that other object is a non-handle object it too will be cleared from memory;
> if it is a handle object, its delete method (if any) will be called, then it
> will be cleared from memory. Thus in neither of those situations do you,
> the user, need to do anything special.
>
> When a handle object is no longer accessible, the same procedure as I
> described above happens, except that its delete method is called before it
> is cleared from memory. The reason the text above calls out non-handle
> objects, I believe, is because they operate somewhat differently than users
> may expect based on their experience with other object-oriented languages
> (in that they don't have destructors.)
>
> --
> Steve Lord
> slord@mathworks.com
>

Hi Steve,
One of the things I'm experiencing with my handle objects is that, when deleted, they are no longer in the workspace, but the memory isn't freed (or not all of it is). If I run "memory" before and after I delete the object and the net difference is smaller than the difference when creating. I am working on a continuously running deployed application that needs to run for long periods of time. I'm experiencing incremental memory increases with each callback run, can't figure out the source of the increases, and have not found a way to release the memory without closing and reopening the application.
Thanks!
~Andrew

Subject: OO matlab - deleting objects

From: Steven_Lord

Date: 27 Feb, 2013 22:20:29

Message: 7 of 7



"Andrew Groth" <andrew.groth@ge.com> wrote in message
news:kglrg4$8ua$1@newscl01ah.mathworks.com...
>> No.
>>
>> When a non-handle object is no longer accessible, it and all its
>> properties are cleared from memory just like normal variables when they
>> go out of scope (like when the function whose workspace they are in
>> exits.) If that non-handle object contains another object in one of its
>> properties, then if that other object is a non-handle object it too will
>> be cleared from memory; if it is a handle object, its delete method (if
>> any) will be called, then it will be cleared from memory. Thus in
>> neither of those situations do you, the user, need to do anything
>> special.
>>
>> When a handle object is no longer accessible, the same procedure as I
>> described above happens, except that its delete method is called before
>> it is cleared from memory. The reason the text above calls out
>> non-handle objects, I believe, is because they operate somewhat
>> differently than users may expect based on their experience with other
>> object-oriented languages (in that they don't have destructors.)
>>
>> --
>> Steve Lord
>> slord@mathworks.com
>
> Hi Steve,
> One of the things I'm experiencing with my handle objects is that, when
> deleted, they are no longer in the workspace, but the memory isn't freed
> (or not all of it is). If I run "memory" before and after I delete the
> object and the net difference is smaller than the difference when
> creating. I am working on a continuously running deployed application that
> needs to run for long periods of time. I'm experiencing incremental memory
> increases with each callback run, can't figure out the source of the
> increases, and have not found a way to release the memory without closing
> and reopening the application.

It's going to be difficult to diagnose this in the abstract. One potential
avenue for investigation would be to check that that nothing (like an
anonymous function handle or another handle object) still "knows about" the
handle object and is keeping it alive, but there are others.

The easiest way to determine what's going on will probably be for you to
send a sample (small) object and a small program with which you can
reproduce this behavior to Technical Support and work with them to determine
what's causing this memory retention. Once you've done so, perhaps you could
return to this thread and summarize what you and Technical Support have
learned.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for 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