I have asked this question before on the Wolfram community forum (http://community.wolfram.com/groups/-/m/t/83845?p_p_auth=Gxoxe65B) but not with great success.
Module creates temporary variables of the form x$NNN which when no longer used are removed. However under certain conditions the temporary variables created by Module are not removed and remain in the global context. If this happens for a Module inside a frequently used function, this leads to memory leak situation where the memory usage of the kernel grows linearly with time. (Which is not good when doing numerical runs of several days).
In certain situations, this behaviour is explainable and probably a feature. Generally, speaking it usually involves some side effects which cause something outside the module to reference the temporary variable.
Some examples
1.
Module[{x}, x /. NDSolve[x'[t] == 1 && x[0] == 0, x, {t, 0, 1}][[1]]]
Result: x$NNN is not removed.
Reason: NDSolve stores some expressions referencing x$NNN in the system cache.
Solution: Clear system cache. (Presumably this will happen on its own as the kernel session progresses.)
2.
Module[{y, z, a, b}, a = y + z; b = 2*a];
Result: y\$NNN and z\$NNN are not removed from global context.
Reason: y\$NNN and z\$NNN appear in the module output and are thus referenced by Out.
Solution: Clear Out, or more systematically limit the History Length through $HistoryLength
3.
Module[{x}, f = x]
Result: x\$NNN is not removed from global context.
Reason: Definition of f is referencing x\$NNN
Solution: Clear f.
Behaviour that I do not understand
In addition to these well-understood cases, there some cases where I do not understand why mathematica is not removing the temporary variables after use:
4.
(from a different thread I found when looking for an answer)
a[b_] := Module[{x}, x := 1; x /; b]
a[True]
Result:a x$NNN is permanently placed in the global context every time a[True] is called.
Reason: Unknown. This appears to be a bug. Possibly related to how mathematica handles conditionals combined with Module (Clear and ClearSystemCache[] do not remove the x$NNN.)
5.
Module[{a, b},
a[i_] := b[i - 1];
b[i_] := a[i - 1]]
Result: a\$NNN and b\$NNN remain in global context.
Reason: Unknown. Bug? Related to circular references of temporary variables.
Edit: Some experimentation has lead me to simplify this example. It has confirmed to me that this must be related to circular referencing since the temporary variables disappear after clearing their definitions with
Clear["a\$NNN", "b\$NNN"]
It is essential that the variable names are input as a string, to avoid referencing them from the input history.
Interestingly enough both temporary variables remain if you clear only one of the definitions using:
Clear["a\$NNN"]
or
Clear["b\$NNN"]
This behaviour seems very buggy to me. At least a cannot think of a reason why this should happen by design.
Other cases
In addition to these cases I have seen this happen for more complicated functions involving Module, which I have not been able to trace to a simple cause.
It would be incredibly helpful for me to understand why this is happening (and how to avoid it). And related if it cannot be avoided: What is the best way of dealing with the stray temporary variables?