How much memory does LuaTeX use?

I've had issues compiling documents this week using LuaTeX on my laptop, where LuaTeX seems to gobble up as much memory as it can, totally crashing the laptop—i.e. requiring a hard reset as it's unresponsive after >10 minutes. The laptop has 6 GB available, but obviously not all of that should be used by LuaTeX when I've got GNOME, Chromium, several PDFs, etc open at the same time.

The documents in question all utilise TikZ externalisation, and usually have about 3 or 4 TikZ images as well as a few other `\includegraphics`, and generally are only about 10 pages in length. The crash seems to occur when LuaTeX forks (?) another run to compile the TikZ images. Watching the system performance with `htop`, I'll often see LuaTeX consuming > 15–20% of my system memory (~1700 MiB) per process.

How do I limit how much memory LuaTeX can use?

I don't really know what other information to provide, as testing with a MWE doesn't consume such ridiculous resources, and in the past community members haven't exactly been happy with me sharing an entire malfunctioning document.

EDIT Further information:

My system is TeXLive 2017, with LuaTeX 1.04. I haven't run an update using `tlmgr` since updating TeXLive last year using the recommended clean install method.

The document uses the KOMA `scrartcl` class, and the packages `fontspec`, `xcolor`, `graphicx`, `etoolbox`, `microtype`, `physics`, `xfrac`, `mathtools`, `unicode-math`, `siunitx`, `isodate`, `scrpage2`, `caption`, `subfig`, `tikz`, `pgfplots`, `pgfplotstable`, `enumitem`, `array`, `tabulary`, `multirow`, `multicol`, `booktabs`, `floatrow`, `biblatex`, `glossaries`, `hyperref`, `hypcap`, and `cleveref`. Most of these are the stock set of packages I nearly always use in documents, copying preambles along and commenting out the packages I know I won't be using. TikZ itself loads the following libraries:

``````\usepgflibrary{luamath}
\pgfkeys{pgf/luamath=parser}
\usepgflibrary{fpu}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{intersections}
\usetikzlibrary{positioning}
\usetikzlibrary{external}
\usetikzlibrary{patterns}
\usepgflibrary{arrows.meta}
\usetikzlibrary{bending}
\usetikzlibrary{angles}
``````

These are the images that were in one particularly troublesome document:

The most complex image uses lots of named coordinates to ensure everything is aligned correctly, and save me the bother of calculating coordinates manually. I originally didn't externalise this image.

Compiling the document on my desktop computer isn't as much of an issue, as the desktop has 12 GB of RAM so a process that uses 1700 MB doesn't bother it quite as much. I still feel though that this is an exceptional amount of memory.

• Do you have the same issue if you compile your document with XeTeX (if that's possible in your case, i.e. you're not using any LuaTeX-specific features)? – ShreevatsaR Apr 29 '18 at 6:54
• How complex are these images? Are we talking plots using lots (1000s) of data points? If not, it's likely there is something up in your TeX input. – Joseph Wright Apr 29 '18 at 7:09
• How old is your LuaTeX executable? Until 2 or 3 years ago, there appeared to be some memory leakage issues. However, i haven’t experienced such problems in TeXlive 2917 and 2018. – Mico Apr 29 '18 at 7:29
• unlike pdftex luatex uses dynamic allocation so it will use as much memory as it needs, if you give it a big enough tikz picture (or an infinite loop) it will allocate all the resources that the operating system allows it to have. – David Carlisle Apr 29 '18 at 7:57
• if this will run on your desktop, at the end of the log there will be a report of the memory used. that should give you a hint as to what is being required of your laptop for this job. – barbara beeton Apr 29 '18 at 15:38

1 Answer

When running on a Linux system, you can limit the memory consumption of LuaTeX using the program `ulimit`. In the example I set the max memory size (`-m`) and the virtual memory (`-v`) to 100000 kilobytes each.

``````\$ ulimit -m 100000 -v 100000
\$ lualatex test.tex
This is LuaTeX, Version 1.07.0 (TeX Live 2018)

...

[1fatal: memory exhausted (xmalloc of 2802244 bytes).
``````

As you can see this is way too little and LuaTeX will crash because it runs out of memory.