5

The TeX engine was originally written in Pascal before dynamic memory allocation was generally available. Modern implementations tend to be done in C. To qualify as TeX an implementation has to pass a standard test, which probably does not pay attention to string types and memory usage.

Consider the following:

\documentclass{article}
\begin{document}
\def\buffer{x}%
\let\test=\buffer
\edef\buffer{\buffer,x}%
\verb$\buffer$ = \buffer

\verb$\test$ = \test
\end{document}

The question is, does the \let make a copy of \buffer or does it simply point to where \buffer is currently stored in memory? If the former, TeX would need to practice dynamic memory allocation on a string level. If the latter, every string ever allocated remains in memory until the program is done. This would explain why putting too many tikzpictures uses up all the resources.

closed as off-topic by siracusa, Phelype Oleinik, Troy, Stefan Pinnow, Sebastiano Sep 14 '18 at 5:45

  • This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    not sure what your test file is trying to demonstrate, but memory for token list is reclaimed once no more csnames point to the list, tex.web says: Examples such as $$\.{\\def\\m\{\\def\\m\{a\}\ b\}}$$ explain why reference counts would be needed even if \TeX\ had no \.{\\let} operation: When the token list for \.{\\m} is being read, the redefinition of \.{\\m} changes the |eqtb| entry before the token list has been fully consumed, so we dare not simply destroy a token list when its control sequence is being redefined. – David Carlisle Jul 15 '15 at 21:45
  • 1
    Well, I would assume, that \let does indeed make copy of the code. Otherwise \let\A\B and redefining \B later on would have the same code for \A and \B (like pointers). Or is this too easy? – user31729 Jul 15 '15 at 21:48
  • A tikzpicture wouldn't normally stress token memory so much but can use a lot of box/node memory that can't be reclaimed until the page is shipped out, so if you have a lot of tikz on the page (or in pending floats) you are holding a lot of nodes in memory. – David Carlisle Jul 15 '15 at 21:56
  • 4
    Roughly speaking: TeX implements its own local dynamic allocation in statically allocated (from OS point of view) array called "main memory". The token lists (macro bodies), generated boxes etc. are saved here. The eqtb table is a list of pointers to meanings of csnames (meanings are stored in main memory if they are macros). TeX disposes the memory by free_avail() function (section 121 of tex.web) if the last pointer in eqtb table to this part of main memory is removed or if saved boxes are not needed any more. \let\a=\b creates only new item in eqtb table, doesn't copy the meaning. – wipet Jul 16 '15 at 6:15
  • 4
    I'm voting to close this question as off-topic because the question was answered in the comments – siracusa Sep 14 '18 at 0:47