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?

 Akzeptierte Antwort

Paul
Paul vor etwa 3 Stunden
Verschoben: Matt J vor etwa 2 Stunden

1 Stimme

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 Kommentare

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 vor etwa 2 Stunden
Or clear all I believe

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Paul
Paul vor etwa 17 Stunden

0 Stimmen

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 Kommentare

Matt J
Matt J vor etwa 4 Stunden
Bearbeitet: Matt J vor etwa 4 Stunden
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 vor etwa 11 Stunden
Bearbeitet: Paul vor etwa 2 Stunden
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 vor 17 Minuten
You can't post mathworks-written code wholesale, but original code should be fine.

Melden Sie sich an, um zu kommentieren.

Kategorien

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

Produkte

Version

R2024b

Tags

Gefragt:

am 17 Jun. 2026 um 21:24

Kommentiert:

vor etwa 9 Stunden

Community Treasure Hunt

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

Start Hunting!

Translated by