Memory and ownership
====================
A budget is an accounting contract. It has meaning only when the lifetime
of a charge matches the lifetime of the storage it pays for.
.. graphviz::
:class: graphviz-adaptive
:caption: The host reserves working storage before allocating it.
:alt: A managed budget accounts for arenas and conversion buffers, backed by RAM or memory-mapped disk.
digraph memory_architecture {
graph [rankdir=TB, bgcolor="transparent", pad="0.22", nodesep="0.3", ranksep="0.35"];
node [shape=box, style="rounded,filled", fillcolor="#edf5f2", color="#216553",
fontname="Helvetica", fontcolor="#16382f", fontsize=12, margin="0.18,0.1"];
edge [color="#216553", fontname="Helvetica", fontcolor="#16382f", fontsize=10, arrowsize=0.7];
budget [label=<Managed host budget
--budget: default 1024 MiB
Reserve before allocating>];
work [label=<Working storage
Arenas, node tables, text buffers>];
backing [class="gd-output", shape=cylinder, fillcolor="#eef4fa",
label=<Storage backing
RAM or memory-mapped disk>];
budget -> work [label="accounts for"];
work -> backing [label="uses"];
}
.. graphviz::
:class: graphviz-adaptive
:caption: Allocation accounting and process isolation are different boundaries.
:alt: Managed host storage shares a process with native libraries. Installed plugins and the development Typst backend use child processes.
digraph allocation_scopes {
graph [rankdir=TB, bgcolor="transparent", pad="0.22", nodesep="0.3", ranksep="0.35"];
node [shape=box, style="rounded,filled", fillcolor="#edf5f2", color="#216553",
fontname="Helvetica", fontcolor="#16382f", fontsize=12, margin="0.18,0.1"];
edge [color="#216553", fontname="Helvetica", fontcolor="#16382f", fontsize=10, arrowsize=0.7];
managed [label=<Host: metered working storage
Build arenas and conversion buffers>];
native [class="gd-memory", fillcolor="#f3eef7", color="#88719d",
label=<Host: other native allocations
Graphviz, tree-sitter, embedded Typst
Outside the host ledger>];
child [class="gd-memory", fillcolor="#f3eef7", color="#88719d",
label=<Child: its own address space
Installed plugins; development Typst
Own RSS, not a host-budget charge>];
managed -> native -> child [style=invis];
}
A charge follows storage
------------------------
Reserve bytes before allocating. Release the charge after freeing the storage.
When replacing a buffer, the old and new buffers can coexist.
Charge the full new allocation during that overlap.
Charging only the increase would understate the peak.
.. math::
\text{replacement peak} = \text{other live storage}
+ \text{old buffer} + \text{new buffer}.
Session-owned input is reclaimed by the session.
Caller-owned input remains the caller's responsibility.
The names ``read_owned`` and ``read_kept`` make that distinction explicit.
A shorter read does not shrink the allocation already made for it.
Known zero is not unknown
-------------------------
A memory probe can know that no headroom remains.
It can also fail to discover headroom. Those are different states.
The policy carries a separate knowledge flag rather than using zero for both.
On Linux, readable cgroup limits apply through the visible ancestor hierarchy.
A container's host-wide free-memory number alone can be misleading.
Reports outlive workspaces
--------------------------
A report shown after a session ends must own its title, source, and repair text.
It must not borrow a buffer that the session has freed.
Source spans use byte offsets internally. User-facing columns count Unicode scalars.
Terminal markers account for display width separately.
The remaining boundary
----------------------
Guidedog meters its managed working storage.
The Typst compiler and native dependencies manage their own allocations.
Graphviz and tree-sitter run native code in the host process. The release's
embedded Typst compiler does too; the development backend can use a subprocess.
An installed plugin runs in a separate process. These are different isolation
boundaries, even though their native allocations fall outside the host ledger.
Large PDF books can consume much more memory than the host budget.
The remote CPython validation measured about 14.5 GiB peak RSS including Typst.
Treat that measurement as workload evidence, not a universal upper bound.
See :doc:`../errors` for the user's choices when a managed budget is reached.