Wolfram Language Paclet Repository

Community-contributed installable additions to the Wolfram Language

Primary Navigation

    • Cloud & Deployment
    • Core Language & Structure
    • Data Manipulation & Analysis
    • Engineering Data & Computation
    • External Interfaces & Connections
    • Financial Data & Computation
    • Geographic Data & Computation
    • Geometry
    • Graphs & Networks
    • Higher Mathematical Computation
    • Images
    • Knowledge Representation & Natural Language
    • Machine Learning
    • Notebook Documents & Presentation
    • Scientific and Medical Data & Computation
    • Social, Cultural & Linguistic Data
    • Strings & Text
    • Symbolic & Numeric Computation
    • System Operation & Setup
    • Time-Related Computation
    • User Interface Construction
    • Visualization & Graphics
    • Random Paclet
    • Alphabetical List
  • Using Paclets
    • Get Started
    • Download Definition Notebook
  • Learn More about Wolfram Language

WeakCache

Guides

  • Weak Cache Functions

Symbols

  • CheckWeakCache
  • CleanupAfter
  • ClearHistory
  • ClearWeakCache
  • CreateReference
  • SameInstanceQ
  • SetWeakCache
  • StrongReference
  • WeakHashTable
  • WeakReference
JasonB`WeakCache`
CleanupAfter
​
CleanupAfter
[expr,func]
evaluates
func
when there are no more references to
expr
in a Wolfram Language session.
​
Details and Options

Examples  
(3)
Basic Examples  
(2)
As in many examples for this paclet, we set the evaluation history to zero to avoid confusion:
The most basic usage of
CleanupAfter
is as follows:
In[1]:=
$HistoryLength=0;​​expr=RandomGraph[{4,5}];​​
CleanupAfter
[expr,Print["Cleaning up after expr"]]
When clearing the expression the cleanup function is called:
In[2]:=
expr=.
Cleaning up after expr
Cleanup functions apply to particular instances of expressions:
In[3]:=
CleanupAfter
[expr1=Range[20],Print["expr1 deleted"]]​​
CleanupAfter
[expr2=Range[20],Print["expr2 deleted"]]​​SameQ[expr1,expr2]
Out[3]=
True
In[4]:=
expr2=.
expr2 deleted
In[5]:=
expr1=.
expr1 deleted
In the above example the two expressions were SameQ but not SameInstanceQ. In the following example expr1 and expr2 both point to the same expression instance:
In[6]:=
CleanupAfter
[expr1=Range[20],Print["expr1 deleted"]]​​
CleanupAfter
[expr2=expr1,Print["expr2 deleted"]]​​
SameInstanceQ
[expr1,expr2]
Out[6]=
True
Both of the added cleanup functions will be called when all references to the data are gone:
In[7]:=
expr1=.
In[8]:=
expr2=.
expr1 deleted
expr2 deleted
​
Set the history length to the default value and evaluate the same code as above:
In[1]:=
$HistoryLength=∞;​​g=RandomGraph[{4,5}]
Out[1]=
In[2]:=
CleanupAfter
[g,Print["g has been cleared"]]
Clearing the symbol g does not remove all references to the object:
In[3]:=
g=.
The value associated with g is still stored in the history:
In[4]:=
%%%
Out[4]=
Only by clearing the history is the memory freed:
In[5]:=
ClearHistory
[]
g has been cleared
Applications  
(1)

SeeAlso
ClearHistory
 
▪
$HistoryLength
 
▪
WeakHashTable
RelatedGuides
▪
Weak Cache Functions
""

© 2025 Wolfram. All rights reserved.

  • Legal & Privacy Policy
  • Contact Us
  • WolframAlpha.com
  • WolframCloud.com