No, it does not cost extra memory to pass expressions. As is usual in functional languages, Mathematica objects are immutable: they cannot be modified, instead a new object is created when you transform them using some function. This also means that if you don't transform them, they're not copied, no matter how much you pass them around between functions.
From a user perspective, Mathematica expressions are trees, but I believe that internally they're stored as directed acyclic graphs, i.e. the same subexpression may be stored only once in memory, regardless of how many times it appears in the full expression (see e.g. the doc page of Share[]
).
Here's an example to illustrate:
First, make sure In
/Out
don't take up extra memory:
In[1]:= $HistoryLength = 0;
Check memory usage:
In[2]:= MemoryInUse[]
Out[2]= 13421756
Let's make an expression that takes up a noticeable amount of memory:
In[3]:= s = f@Range[1000000];
In[4]:= MemoryInUse[]
Out[4]= 17430260
Now repeat this expression a hundred times ...
In[5]:= t = ConstantArray[s, 100];
... and notice that memory usage barely increases:
In[6]:= MemoryInUse[]
Out[6]= 18264676
ByeCount[]
is misleading because it doesn't report the actual physical memory used, but the memory that would be used if common subexpressions weren't allowed to share the same memory:
In[7]:= ByteCount[t]
Out[7]= 400018040
An interesting point to note: if you remove f[...]
from s
, and make both s
and t
a plain numerical array, then this memory sharing will not happen, and memory usage will jump to ~400 MB.
Whether you make tree
a global variable or an argument of getChildren
, it will not make a difference in memory usage.
tree=5
thentree
is 5 everywhere in theGlobal
context (which is the default). it's global by default, unless you're after some other behaviour. – Factory