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?

 Accepted Answer

Paul
Paul 40 minutes ago
Moved: Matt J 27 minutes ago
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 Comments

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
Or clear all I believe

Sign in to comment.

More Answers (1)

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 Comments

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 about 9 hours ago
Edited: Paul 30 minutes ago
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.
You can't post mathworks-written code wholesale, but original code should be fine.

Sign in to comment.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Products

Release

R2024b

Tags

Asked:

about 22 hours ago

Commented:

about 3 hours ago

Community Treasure Hunt

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

Start Hunting!