Filter löschen
Filter löschen

Hidden memory?

7 Ansichten (letzte 30 Tage)
Stephen Shank
Stephen Shank am 9 Apr. 2012
In the course of coding I came across the following curious example:
classdef lu_apply
function obj=lu_apply(A)
obj.f=@(X) U\(L\X);
The following test run is confusing me:
>> A=rand(100); x=rand(100,1); foo=lu_apply(A);
>> whos
Name Size Bytes Class Attributes
A 100x100 80000 double
foo 1x1 136 lu_apply
x 100x1 800 double
>> norm(foo.f(x)-A\x)
ans =
What's bothering me is that it would seem as though the L and U have to be stored somewhere, but it's not at all clear to me where that is. The only other possibility would be to compute the L and U at each call, but the LU routine is only called when the class is constructed. My questions are... where are the L and U located, if anywhere? And if they are somewhere, when is the memory freed? Also, is there anyway to remedy this, should I ever want to free the memory manually (first thing that comes to mind is to declare L and U to be global)?

Akzeptierte Antwort

James Tursa
James Tursa am 10 Apr. 2012
To add to what Daniel has already written, consider these lines:
function obj=lu_apply(A)
obj.f=@(X) U\(L\X);
The L and U matrices get created inside the function lu_apply. They are local to the function and will get destroyed once the function returns. The following line:
obj.f=@(X) U\(L\X);
creates a function handle using the local L and U variables as part of the function definition. At that time, shared data copies of L and U are created and become embedded in the data area of the function handle ... i.e., they essentially become part of the function handle variable itself. Basically these shared data copies of L and U become snapshots of the current state of L and U at the time of the function handle creation and will remain as constants as far as the function handle is concerned.
Whatever happens to L and U downstream doesn't affect this function handle anymore. In particular, when the function returns, the local variables L and U are destroyed, but the shared data copies of them that were created earlier still live on (and use memory) as part of the function handle. The only way to clear this memory is to clear the function handle itself. I don't know of any way to directly interrogate a function handle to determine the sizes of the embedded variables it contains, since I am not familiar with how function handle definitions are encoded in the data area of the variable.

Weitere Antworten (1)

Daniel Shub
Daniel Shub am 9 Apr. 2012
The whos function tells you about the variables in the current workspace. The variables L ad U exist, but not in any workspace that you can get to. The memory that L and U consume will be cleared when the last reference to them is cleared. In this case there is a hidden reference to L and U in the function handle.
I believe that if you chose to use an environment like MATLAB, you need to be willing to give up control of memory management.


Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by