This issue is specifically different from the "duplicate" question. The creation of variables in the active context during parsing is of no surprise and is the expected behavior.

What I'm observing is the creation of a temporary variable which is not automatically deleted. The following constructs:

f[x_] := With[{a = x}, dostuff];

or more generally

f[x_] := Module[{a = g[x]}, dostuff];

create not only a in the global namespace, but also a$:

Attributes[a$] = {Temporary}

Should I be concerned, is this a memory leak, is there specific reasoning behind this, etc?
END EDIT; original question below

Starting with a fresh kernel I type:

myFunc[x_] := With[{g = x}, Print[5]];
? Global`*

Among the returned symbols I find, not only the expected myFunc, g, and x. There is also the temporary in attribute g$. Why is this happening? Is this indicating some sort of memory leak?

The implicit question is also "how to avoid this and keep my packages clean if this is indeed the case".

m_goldberg asked for a more realistic use case; I'm more than happy to oblige:

ImportBIG[names_List, p0_Real, a0_Real, depol_Real, fourierFunc_]:=
        {strm = OpenRead[#, BinaryFormat->True] & /@ names , n = Length[names],
            fft = (Exp[2 I a0 Degree - 2 I Pi (-1+(ConstantArray[0.I,20]+Range[20]))2/20])/20
            Do[assoc[idx] = ReadString[strm[[idx]]];
                dostuff;, {idx, n}]
            (* more do loops with {idx, n} *)
            Do[assoc[idx, "Rho"] = With[{f = assoc[idx, "Fourier"]},
                ArcTan[-I Sqrt[1 - 4 Abs[f]^2] + 2 Im[f]] / (1 - 2 Re[f])]], {idx, n}]

As you can see, I'm using With as an idiomatic way to have constants in my functions, also to shorten on keystrokes to make code more readable, to cut down on repeated evaluation of the same thing (such as Length[names]), etc.

In this case, of course, the offending temporary variables are n$, fft$, strm$ and f$.

  • 1
    $\begingroup$ Can you give an example where this kind of construct is needed? I ask because formal arguments (x in your case) have semantics very close to what With provides and I don't see why you need to use With like you show in your question. $\endgroup$
    – m_goldberg
    Nov 11, 2016 at 9:20
  • 1
    $\begingroup$ Overeager localization? This is one of the ways name conflicts are avoided e.g. in With[{x = 5}, Function[x, x^2]] where one of the two xs must be renamed to prevent conflict between the With and the Function. In your example this doesn't seem to be needed but happens anyway. That's much better than when it is needed but doesn't happen. It doesn't cause harm. I don't know why the Temporary isn't removed (as it is with Module ones), but it has been like this forever. $\endgroup$
    – Szabolcs
    Nov 11, 2016 at 9:38
  • 1
    $\begingroup$ Related: "Module trash collection behaviour", "Reducing memory usage in an extended Mathematica session." $\endgroup$ Nov 11, 2016 at 10:51
  • 1
    $\begingroup$ Related: (119403). Also (20766), (42351), (91917). $\endgroup$
    – Michael E2
    Nov 11, 2016 at 11:07
  • 1
    $\begingroup$ No, I don't think you should be concerned. It's not a memory leak if var$ has no value. It's not causing any problems. It doesn't produce an unlimited amount of pollution like numbered Module variables would since the name is always the same: var$, instead of var$1, var$2, var$3 ... with no end to it. While the question can be interesting theoretically, I see no practical issues. $\endgroup$
    – Szabolcs
    Nov 14, 2016 at 14:10


Your Answer

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

Browse other questions tagged or ask your own question.