warp.ScopedMemoryTracker#

class warp.ScopedMemoryTracker(
name='root',
active=True,
print=True,
report_func=None,
)[source]#

Context manager that tracks memory allocations across all devices.

Tracking is performed in the C++ native layer by intercepting all wp_alloc_* / wp_free_* calls, so both Python-originated and C++ internal allocations (BVH, HashGrid, etc.) are captured. C++ allocations are labeled with the originating subsystem (e.g. (native:bvh)), while Python allocations include the call-site file, line, and function name.

Parameters:
  • name (str) – Scope name for grouping allocations. Nested trackers form a hierarchical scope path, e.g. "simulation/collision".

  • active (bool) – If False, the tracker becomes a no-op. This allows keeping tracker calls in the code and toggling them without changing the call sites.

  • print (bool) – If True (the default), print an allocation summary on exit.

  • report_func (Callable[[str], None] | None) – Optional callback (str) -> None for custom report handling. When set, the report text is passed to this function instead of being printed to sys.stdout.

Example

with warp.ScopedMemoryTracker("my_scope") as tracker:
    a = warp.zeros(1000, dtype=warp.float32, device="cpu")
    b = warp.zeros(1000, dtype=warp.float32, device="cuda:0")
# prints allocation summary automatically
__init__(
name='root',
active=True,
print=True,
report_func=None,
)[source]#
Parameters:

Methods

__init__([name, active, print, report_func])

clear()

Reset all tracking data while keeping the tracker active.

report([file, sort, max_items])

Print an allocation report.

report(file=None, sort='size', max_items=10)[source]#

Print an allocation report.

Can be called multiple times – each call reflects the current state.

Note

Not safe to call concurrently from multiple threads.

Parameters:
  • file – File object to write to (defaults to sys.stdout).

  • sort – Sort order for live allocations: "size" (default, largest first) or "chronological" (oldest first).

  • max_items – Maximum number of individual allocations shown per category (default 10).

Raises:

ValueError – If sort is not "size" or "chronological".

clear()[source]#

Reset all tracking data while keeping the tracker active.

After calling this, subsequent report() calls only reflect allocations that occurred after the reset.