Memory profiling
Quick answer
Memory profiling measures allocation behavior and resource pressure. Tensor payload bytes, traced allocations, resident process memory and accelerator memory are different measurements. Examples use Python and NumPy 2.3.5. Timing examples use fixed synthetic values; actual benchmark timings are in the milestone. ByteMind execution is unverified.
Array payload bytes
import numpy as npa=np.zeros((3,4),dtype=np.float32)print(a.nbytes,a.astype(np.float16).nbytes)Expected output (locally tested): 48 24. Two dtypes store different bytes per element.
How it works
A dense array with shape (b,d) and item size s has bds logical element bytes. NumPy ndarray.nbytes reports element bytes, not complete process usage. Views may share storage, so summing their nbytes can double count. Temporary intermediates, allocator caches, runtime libraries, fragmentation and parallel requests add overhead. Python tracemalloc traces allocations that are registered with its tracing machinery; it is not a complete native or accelerator memory meter. Separate timing from profiling because tracing changes execution overhead. Record baseline, peak, scope, batch shape and dtype; measure resident memory using an appropriate platform tool if needed, without treating logical bytes as RSS. In this lab compare NumPy weight payloads and observed traced peak in a separate pass. Measure a fixed batch, not an unbounded stream. Memory headroom matters even if average latency is excellent.
Shared view
import numpy as npa=np.arange(8,dtype=np.int32); b=a[::2]print(a.nbytes,b.nbytes,np.shares_memory(a,b))Expected output (locally tested): 32 16 True. Logical bytes do not imply independent storage.
Common mistakes
Do not report sys.getsizeof or ndarray.nbytes as complete process memory. Do not sum shared views as independent allocations.
Practice and next steps
Compare a 32 by 64 array in float32 and float16. Explain what is missing from the resulting byte counts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Memory profiling — knowledge check
Apply memory profiling to small examples and debugging scenarios.
What does ndarray.nbytes report?
What can cause double counting?
Why profile separately from timing?
Which is not measured by logical payload bytes?
What is a useful memory report?