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?

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

2 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 1 hour ago
Edited: Paul 31 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 an m-function (at least in 2024a) and I was able to start from there and get the information you seek.

Sign in to comment.

Categories

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

Products

Release

R2024b

Tags

Asked:

about 14 hours ago

Edited:

about 8 hours ago

Community Treasure Hunt

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

Start Hunting!