LightningWeb.Live.MemoryDebug (Lightning v2.14.11-pre1)

View Source

Memory debugging utilities for LiveViews to track memory usage and help diagnose OOM issues.

Usage in a LiveView:

import LightningWeb.Live.MemoryDebug

def mount(params, session, socket) do
  log_memory("mount start")

  # ... your code ...

  socket = assign(socket, :data, large_data)
  log_memory("after loading large data", socket: socket)

  {:ok, socket}
end

Summary

Functions

Compares two memory snapshots and logs the difference.

Logs current memory usage for the calling process and optionally the socket assigns.

Wraps a function call with before/after memory logging.

Measures memory of specific socket assigns.

Gets a memory snapshot as a map (useful for comparing before/after).

Functions

compare_snapshots(before, after_snap, label)

Compares two memory snapshots and logs the difference.

log_memory(label, opts \\ [])

Logs current memory usage for the calling process and optionally the socket assigns.

Options

  • :socket - Phoenix.LiveView.Socket to measure assigns memory
  • :assigns - List of assign keys to measure individually (e.g., [:dataclip, :workflow])

measure(label, fun)

Wraps a function call with before/after memory logging.

Examples

measure("load dataclip", fn ->
  Invocation.get_dataclip!(id)
end)

measure_assigns(socket, keys)

Measures memory of specific socket assigns.

snapshot()

Gets a memory snapshot as a map (useful for comparing before/after).