Hidden memory?
이전 댓글 표시
In the course of coding I came across the following curious example:
classdef lu_apply
properties
f;
end
methods
function obj=lu_apply(A)
[L,U]=lu(A);
obj.f=@(X) U\(L\X);
end
end
end
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 =
0
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)?
채택된 답변
추가 답변 (1개)
Daniel Shub
2012년 4월 9일
1 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Platform and License에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!