MemViz is a modern, header-only C++20 library for memory introspection and runtime diagnostics.
It combines compile-time reflection with runtime inspection utilities, giving developers deep visibility into how objects are laid out and managed in memory.
π Why MemViz?
C++ is powerful but opaque when it comes to memory. Developers often struggle with:
- Understanding how compilers lay out structs and classes.
- Debugging memory leaks and tracking allocations.
- Inspecting vtables and low-level object representations.
- Teaching or documenting C++ memory models in an accessible way.
MemViz bridges this gap by providing transparent, lightweight tools to explore memory safely and directly.
π¦ Core Features
- π§© Reflection without intrusive macros
Register members of your types using MEMVIZ_REGISTER once, then access offsets and layout at runtime.
- π Layout inspection
Print struct/class size, alignment, location, and member offsets with a single call to dumpLayout<T>().
- π‘ Safe casting utilities
Use castAndPrint for compile-timeβchecked type conversions, backed by std::bit_cast.
- π VTable exploration
Inspect virtual function tables (inspectVTable) to understand polymorphic object layouts.
- π§ Allocation tracking
Catch leaks with reportLeaks and visualize current allocations with printAllocationLog.
- β‘οΈ Zero dependencies
Header-only, no linking required. Just include and use.
π‘ Benefits
- Debug Smarter: Detect memory leaks, unexpected allocations, and object lifetime issues early.
- Learn by Seeing: Perfect for students and educators β visualize object memory layouts to understand padding, alignment, and vtables.
- Safer Experimentation: Explore tricky areas of C++ (casting, inheritance, layout rules) without undefined behavior.
- Production-Friendly: Minimal overhead; disable at compile-time when not needed.
- Portable: Works across modern compilers supporting C++20.
π Typical Use Cases
- π Leak detection in unit tests or CI pipelines.
- π Teaching C++ memory model concepts (object model, padding, inheritance).
- π Debugging alignment and ABI issues across compilers/platforms.
- π§ͺ Experimenting with polymorphism and vtable layouts.
- π Visualizing binary layouts for performance tuning or serialization design.
π Example: Layout Inspection
struct Person {
int age;
char gender;
double height;
};
std::make_pair("age", &Person::age),
std::make_pair("gender", &Person::gender),
std::make_pair("height", &Person::height)
));
Person p = {25, 'F', 170.5};
memviz::LayoutInspector::dumpLayout(p);
````
#### Output
#define MEMVIZ_REGISTER(Type, Tuple)
Registers a user-defined type for reflection in MemViz.
Definition Macros.hpp:38
[Layout] Type: Person Size: 16 bytes Alignment: 8 bytes Location: 0x7ffee7... Members: age: offset = 0 gender: offset = 4 height: offset = 8
---
## π Example: Leak Tracking
cpp Foo* a = new Foo(1, "Alpha"); Foo* b = new Foo(2, "Beta"); delete a;
memviz::reportLeaks(); // shows only b as leaked delete b;
memviz::reportLeaks(); // shows 0 leaks
[0] LEAK 0x7fe40e705be0 (32 bytes) [SUMMARY] 1 potential leak(s), total 32 bytes. [SUMMARY] 0 potential leak(s), total 0 bytes. ```
π Roadmap
- π¦ CMake integration for easy consumption.
- π¨ Richer terminal visualization (color-coding, tree views).
- π JSON/CSV output for automated analysis.
- π Optional stack trace capture for allocations.
π Resources
β‘οΈ Summary
MemViz gives you X-ray vision into C++ memory:
- Understand layouts.
- Track allocations.
- Inspect vtables.
- Learn faster.
- Debug smarter.
All without leaving modern, standard C++.