# `Alternera.PluginWasmFramework.MemoryLimit`
[🔗](https://github.com/10e-ab/alternera-wasm/blob/v0.9.1/alternera_plugin_wasm_framework/lib/alternera/plugin_wasm_framework/memory_limit.ex#L1)

A language step's "Memory limit" setting: how much memory the script's
guest may use in one call. Guest memory is outside every BEAM limit, so
the step says it, where whoever edits the pipeline sees it, and it
travels with the step in exports and releases.

    %{"mode" => "flat", "mib" => 4}       # Flat: a number of MiB
    %{"mode" => "body", "factor" => 8}    # Body ×: a multiple of the body

  * **Flat** (`"flat"`), the default at 4 MiB: a field-for-field JSON
    transform measured about 2 MiB on a 100 KB body and 6 MiB on a
    500 KB one, so the default holds bodies up to about 300 KB; a step
    with larger ones raises it or takes Body ×.
  * **Body ×** (`"body"`): a multiple of the size of the body the step
    holds in its direction (the body only, not variables or inputs),
    from the language's starting multiple when someone switches to it
    (`%Language{body_factor: …}`, 8 unless the language says
    otherwise: Lua measured about seven times a decoded JSON body,
    JavaScript about ten, so it starts at 12), never under a floor of
    8 MiB: a multiple of an empty body is nothing, and an interpreter
    needs a few MiB to start. A step that holds no body (`reads_body`
    off) offers Flat only.

Over both sits the node's ceiling per call, `max_memory_mib` in the
framework's config (default 128): a save over it is refused with a
message that names it, and a Body × limit that grows past it on a large
body is held to it.

    config :alternera_plugin_wasm_framework, max_memory_mib: 128

The ceiling protects the node, so it is not a tenant or step setting.

# `t`

```elixir
@type t() :: {:flat, pos_integer()} | {:body, pos_integer()}
```

A checked setting.

# `bytes`

```elixir
@spec bytes(t(), non_neg_integer()) :: pos_integer()
```

The bytes one call may use under `limit` with a body of `body_bytes`:
the setting, the Body × floor under it and the node's ceiling over it.

# `ceiling_mib`

```elixir
@spec ceiling_mib() :: pos_integer()
```

The node's ceiling per call, in MiB (`max_memory_mib`).

# `default`

```elixir
@spec default() :: t()
```

The setting a step without one has: Flat, 4 MiB.

# `default_factor`

```elixir
@spec default_factor() :: pos_integer()
```

The multiple Body × starts at for a language that names none.

# `describe`

```elixir
@spec describe(t()) :: String.t()
```

The setting in words, for a failure and the step's list: "Flat 4 MiB", "Body × 8".

# `floor_mib`

```elixir
@spec floor_mib() :: pos_integer()
```

The least a Body × limit gives, in MiB.

# `parse`

```elixir
@spec parse(term(), boolean(), pos_integer()) :: {:ok, t()} | {:error, String.t()}
```

Checks a stored setting (nil is the default). `reads_body?` says whether
the step holds a body, without which Body × means nothing;
`default_factor` is the multiple a Body × setting without one takes,
the language's.

# `to_settings`

```elixir
@spec to_settings(t()) :: map()
```

The setting stored back: what a step's settings hold.

# `usage`

```elixir
@spec usage(non_neg_integer(), pos_integer()) :: String.t()
```

Used of limit for the step's summary: "21 of 32 MiB", rounded up to whole MiB.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
