List all memoized function caches

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?

 Respuesta aceptada

Paul
Paul hace alrededor de 2 horas
Movida: Matt J hace alrededor de 2 horas
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 comentarios

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 hace alrededor de 2 horas
Or clear all I believe

Iniciar sesión para comentar.

Más respuestas (1)

Paul
Paul hace alrededor de 17 horas
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 comentarios

Matt J
Matt J hace 16 minutos
Editada: Matt J hace 15 minutos
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 hace alrededor de 9 horas
Editada: Paul hace alrededor de 1 hora
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 hace 24 minutos
You can't post mathworks-written code wholesale, but original code should be fine.

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Productos

Versión

R2024b

Etiquetas

Preguntada:

el 17 de Jun. de 2026 a las 21:24

Comentada:

hace alrededor de 6 horas

Community Treasure Hunt

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

Start Hunting!

Translated by