How to stop typst from consuming increasing RAM for a long running watch process?

I watch a PDF which I am working on. At the start it consumes around 600 MB RAM bit as time passes it starts consuming increasing amount of RAM. I have to restart the process periodically. The PDF is around 160 pages long. I am not sure if this is the right place to ask or whether I should create an issue on github.

1 Like

What you see is probably expected behavior: Typst uses memoization (caching the result of function calls) to allow for instant preview but that also means increasing memory use for long running watch sessions. See this blog post for some details: What If LaTeX Had Instant Preview? | Laurenz's Blog

There is already an issue relevant to the problem of excessive memory usage: Ability to selectively disable memoization · Issue #6508 · typst/typst · GitHub

2 Likes

Naively I would expect incrementality (which you need for instant preview) to be achievable by caching only the latest rendering of the whole document. (There are cases of back-and-forth where caching a few extra steps would help, but they are not essential.)

This suggests that leaking memory over time (and over many renders) is not, in fact, a naturally emergent behavior of incrementality, and may also be avoidable without fundamentally changing the memoization-based approach.

(It might require some tighter coordination between the document-rendering driver and the comemo cache-eviction strategy. A given function could be called many times during a single render, and it should be able to memoize all its calls. So I don’t want a per-cache age-based policy as comemo currently supports, but rather the ability to attach a global “epoch” to each cached entry, decided at memoized-function-call time, and then evict all entries whose epoch satisfies a certain condition (for example, not being the last one or the one before that)).

2 Likes