According to the Documentation,

Symbols with attribute Temporary are automatically removed when they are no longer needed.

For example:

$HistoryLength = 0;
Module[{a}, f = a]
f =.

f = a$497

{"a", "a$497", "f"}

{"a", "f"}

One can see that the Temporary symbol a$497 was deleted automatically when the definition for f (which contained it) was removed.

Is is possible to find out which definitions contain given Temporary symbol?

Note that these definitions may be assigned to symbols in any Context, not necessary Global`.


1 Answer 1


Here is an approach based on documented functionality:

$HistoryLength = 0;
ClearAll[whereIsIt, isItHere, valuesList, holders];
SetAttributes[{whereIsIt, isItHere, valuesList, holders}, HoldAllComplete];
(* The complete list of known holders *)
holders = {OwnValues, DownValues, UpValues, SubValues, DefaultValues, FormatValues, NValues};
(* The list of values *)
valuesList[ss_Symbol] := Through[holders[Unevaluated@ss]];
(* Assuming that ReadProtected symbols do not lock Temporary Symbols *)
isItHere[ss_Symbol, s_Symbol] := 
  If[TrueQ[MemberQ[Attributes[ss], ReadProtected]], 
   False, ! FreeQ[valuesList[ss], HoldPattern[s], {-1}]];
(* Finding out where the Symbol is locked *)
whereIsIt[s_Symbol] := 
     ToExpression[#, InputForm, 
      Function[ss, isItHere[ss, s], HoldAllComplete]]} & /@ 
   Names[], {ss_, True} :> ss]

The function whereIsIt can be used as follows:

Module[{t}, d = t]

The drawback of this solution is that it required the Symbol itself and so if one do not switch off history tracking this symbol will be locked in the history as well. Better solution would take only the name of the symbol (a String).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.