Main Content

clearCache

Clear cache for MemoizedFunction object

Description

example

clearCache(mfcn) clears the cache for a MemoizedFunction object.

The memoization of a function is associated with the input function and not with the MemoizedFunction object. Therefore, clearing a variable does not clear the cache associated with the input function.

A MemoizedFunction object is not aware of updates to the underlying function. If you modify the function associated with the memoized function, use clearCache to clear the cache.

Examples

collapse all

Create a memoized function object for the sin function, and call it several times.

mf = memoize(@sin);
a = mf(0);
b = mf(pi/2);
c = mf(0);

View the statistics for the object.

mf.stats.Cache
ans = struct with fields:
         Inputs: {{1x1 cell}  {1x1 cell}}
        Nargout: [1 1]
        Outputs: {{1x1 cell}  {1x1 cell}}
       HitCount: [1 0]
      TotalHits: 1
    TotalMisses: 2

Clear the memoized function object cache, and view the statistics.

mf.clearCache
mf.stats.Cache
ans = struct with fields:
         Inputs: {}
        Nargout: []
        Outputs: {}
       HitCount: []
      TotalHits: 0
    TotalMisses: 0

Input Arguments

collapse all

Function with memoization semantics, specified as a MemoizedFunction object.

Tips

  • Two variables that memoize the same function share a cache and object property values, such as cache size. In the following example, the variables a and b share a cache and have the same value for cache size.

    a = memoize(@svd);
    b = memoize(@svd);
    Clearing the cache for b (b.clearCache) also clears the cache for a, and any other variables that memoize the svd function.

  • Clearing a variable does not clear the cache associated with the input function. To clear the cache for a MemoizedFunction object that no longer exists in the workspace, create a new MemoizedFunction object to the same function, and use the clearCache function on the new object. Alternatively, you can clear caches for all MemoizedFunction objects using the clearAllMemoizedCaches function.

Introduced in R2017a