I know that clearAllMemoizedCaches will clear all memoized function data, but what if I just want to list all existing memoized caches (and maybe also their cache stats). Is there a way to do that?

 채택된 답변

Paul
Paul 2026년 6월 18일 15:58
이동: Matt J 2026년 6월 18일 16:12

1 개 추천

mf1 = memoize(@func1);
mf2 = memoize(@func2);
for ii = 1:10,mf1(ii);mf2(ii);end
clear mf1 mf2
mfs = struct(matlab.lang.internal.Memoizer.getInstance());
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
f = cellfun(@(mfc) mfc.Function,mfs.MemoizedFunctionCache,'Uni',false)
f = 1×2 cell array
{@func1} {@func2}
function f1 = func1(x)
f1 = x;
end
function f2 = func2(x)
f2 = x;
end

댓글 수: 2

Nice. This method also exposes a few interesting things about memoized functions. In particular, clearAllMemoizedCaches doesn't really get rid of a memoization,
memoize(@sin); memoize(@cos);
struct(matlab.lang.internal.Memoizer.getInstance())
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
ans = struct with fields:
MemoizedFunctionCache: {[1×1 matlab.lang.MemoizedFunction] [1×1 matlab.lang.MemoizedFunction]} Tracer: 0
Only clear functions seems to be able to do that,
clear functions
struct(matlab.lang.internal.Memoizer.getInstance())
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
ans = struct with fields:
MemoizedFunctionCache: {} Tracer: 0
Paul
Paul 2026년 6월 18일 16:27
Or clear all I believe

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paul
Paul 2026년 6월 18일 1:31

0 개 추천

Hi Matt,
Is evalin acceptable?
clearvars
clearAllMemoizedCaches;
mf1a = memoize(@func1);
mf1b = memoize(@func1);
mf2 = memoize(@func2);
for ii = 1:10,mf1a(ii);mf2(ii);end
a = pi;
w = whos;
w = w(strcmp({w.class},'matlab.lang.MemoizedFunction'));
s = arrayfun(@(w) evalin('base',"stats(" + w.name + ")"),w);
names = {w.name};
[s(:).Name] = names{:}
s = 3×1 struct array with fields:
Cache MostHitCachedInput CacheHitRatePercent CacheOccupancyPercent Name
function func1(x)
x;
end
function func2(x)
x;
end

댓글 수: 3

Matt J
Matt J 2026년 6월 18일 2:24
편집: Matt J 2026년 6월 18일 2:25
Hi Paul,
I realize now that I didn't make it clear enough what I was after, but a solution based on whos() won't work. The problem is that functions can remain memoized even after all MemoizationFunction objects pointing to them have been deleted. I am looking for a way to list all lingering memoized caches even when this is the case. In other words, I would like to do be able to call a function listAllMemoizedCaches like in the commented section below and get the output shown.
clearAllMemoizedCaches, clearvars
mf=memoize(@func);
y1=mf(10)
Caching
y1 = 10
y2=mf(20)
Caching
y2 = 20
clear mf
% L=listAllMemoizedCaches()
%
% L=
% @func
mf=memoize(@func);
y3=mf(10) %No caching -- original memoizations are still alive
y3 = 10
y4=mf(20)
y4 = 20
function x=func(x)
disp Caching
x;
end
Paul
Paul 2026년 6월 18일 3:06
편집: Paul 2026년 6월 18일 11:42
The question was clear enough. I just misunderstood it.
After some poking around, I think you can get what you want (i.e., a list of function handles) using some undocumented (yet easily found) and unrecommended (yet discussed here on this forum) functionality (I don't see any documented way to do it). Not sure if it would be appropriate to post here. Are there any guidelines for such things?
clearAllMemoizedCaches is an m-function (at least in 2024a) and I was able to start from there and get the information you seek.
Matt J
Matt J 2026년 6월 18일 13:22
You can't post mathworks-written code wholesale, but original code should be fine.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

제품

릴리스

R2024b

태그

질문:

2026년 6월 17일 21:24

댓글:

2026년 6월 18일 16:27

Community Treasure Hunt

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

Start Hunting!

Translated by