memscope-rs
 
A high-performance memory tracking library for Rust applications with modular engine architecture.
๐ v0.2.0 - Major Refactoring
Recent Changes:
- ๐๏ธ Architecture Refactoring: Migrated from monolithic to modular engine architecture
- ๐ Code Reduction: ~75% code reduction (265K lines removed)
- ๐ Enhanced Safety: Eliminated all unsafe
unwrap()calls - โก Performance: Up to 98% improvement in concurrent tracking scenarios
- ๐ Code Stats: 525 files changed, current codebase: 77,641 lines
Architecture
graph TB
subgraph "User Code"
A[track_var! macro]
B[track_scope! macro]
end
subgraph "Facade Layer"
C[Unified Tracker API]
end
subgraph "Engines"
D[Capture Engine]
E[Analysis Engine]
F[Event Store Engine]
G[Render Engine]
H[Snapshot Engine]
I[Timeline Engine]
J[Query Engine]
K[Metadata Engine]
end
subgraph "Backends"
L[CoreTracker]
M[LockfreeTracker]
N[AsyncTracker]
O[GlobalTracker]
end
A --> C
B --> C
C --> D
D --> L
D --> M
D --> N
D --> O
D --> F
E --> F
E --> G
G --> J
H --> F
I --> F
J --> K
Data Flow
sequenceDiagram
participant User as User Code
participant Facade as Facade API
participant Capture as Capture Engine
participant EventStore as Event Store Engine
participant Analysis as Analysis Engine
participant Render as Render Engine
User->>Facade: track_var!(data)
Facade->>Capture: capture_alloc(ptr, size)
Capture->>EventStore: store event
User->>Facade: analyze()
Facade->>Analysis: detect issues
Analysis->>EventStore: read events
Analysis-->>Facade: report
User->>Facade: export_json()
Facade->>Render: render data
Render-->>User: output file
Module Overview
graph LR
subgraph "Core Layer"
facade[facade/]
tracker[tracker/]
end
subgraph "Engine Layer"
capture[capture/]
analysis[analysis_engine/]
event[event_store/]
render[render_engine/]
snapshot[snapshot/]
timeline[timeline/]
query[query/]
metadata[metadata/]
end
subgraph "Analysis Modules"
detectors[detectors/]
safety[safety/]
classification[classification/]
end
facade --> tracker
tracker --> capture
capture --> event
capture --> analysis
analysis --> detectors
analysis --> safety
analysis --> classification
analysis --> snapshot
analysis --> timeline
event --> query
event --> render
Quick Start
use ;
Tracking Backends
| Backend | Use Case | Performance | Notes |
|---|---|---|---|
| CoreTracker | Single-threaded | ~23ns | Simple, low overhead |
| LockfreeTracker | Multi-threaded | ~39ns | Lock-free, thread-local storage |
| AsyncTracker | Async tasks | ~23ns | Task ID tracking |
| GlobalTracker | Global tracking | Variable | Shared across threads |
Engine Capabilities
Analysis Engine
- Leak Detection - Find unreleased allocations
- Use-After-Free Detection - Detect UAF patterns
- Buffer Overflow Detection - Find bounds violations
- Safety Analysis - Risk assessment for unsafe code
- Circular Reference Detection - Detect reference cycles
- Relation Inference - Track variable relationships
Capture Engine
- Multi-backend support - Core, Lockfree, Async, Global
- Smart pointer tracking - Rc/Arc/Box/Weak support
- Thread-local storage - Efficient concurrent tracking
- FFI boundary tracking - Memory passport for FFI calls
Event Store Engine
- Lock-free queue - High-throughput event storage
- Snapshot support - Point-in-time views
- Thread-safe - Concurrent read/write access
Render Engine
- JSON Export - Human-readable format
- HTML Dashboard - Interactive visualization
Other Engines
- Snapshot Engine - Memory snapshot construction
- Timeline Engine - Time-based memory analysis
- Query Engine - Unified query interface
- Metadata Engine - Centralized metadata management
Performance
| Metric | Performance | Improvement |
|---|---|---|
| Concurrent Tracking (1) | 98ยตs | -98% โก |
| Concurrent Tracking (64) | 1.9ms | -25% โก |
| Analysis (100 elements) | 30ยตs | -91% โก |
| Lockfree Allocation | 39ns | -46% โก |
| Type Classification | 40-56ns | 1-21% โก |
See benchmarks/run.log for detailed performance data.
Installation
[]
= "0.2.0"
Migration from v0.1.x
Important Breaking Changes:
- Tracking API moved to
memscope_rs::trackermodule - Error handling system completely refactored
- Some internal modules reorganized
Quick Migration:
// Old API (v0.1.x)
use ;
// New API (v0.2.0)
use ;
Examples
# Basic usage
# Multi-threaded
# Async
# Full showcase with dashboard
# Merkle tree example
# Variable relationships
# Unsafe FFI demo
Documentation
Key Modules
- Analysis Module - Leak detection, relation inference, safety analysis
- Tracker Module - Core tracking API
- Capture Module - Memory capture backends
- Render Engine - Export and visualization
- Core Module - Core types and utilities
Project Structure
src/
โโโ analysis/ # Analysis modules
โ โโโ detectors/ # Leak, UAF, Overflow detectors
โ โโโ safety/ # Safety analyzer
โ โโโ classification/ # Type classification
โ โโโ ... # Other analysis modules
โโโ analysis_engine/ # Analysis engine orchestration
โโโ capture/ # Capture engine and backends
โ โโโ backends/ # Core, Lockfree, Async, Global trackers
โ โโโ types/ # Capture data types
โ โโโ platform/ # Platform-specific implementations
โโโ core/ # Core types and utilities
โโโ error/ # Unified error handling
โโโ event_store/ # Event storage engine
โโโ render_engine/ # Output rendering
โ โโโ dashboard/ # HTML templates
โโโ snapshot/ # Snapshot engine
โโโ timeline/ # Timeline engine
โโโ query/ # Query engine
โโโ metadata/ # Metadata engine
โโโ tracker/ # Unified tracker API
โโโ facade/ # Facade API
โโโ lib.rs # Public API
Comparison with Other Tools
| Feature | memscope-rs | Valgrind | AddressSanitizer | Heaptrack |
|---|---|---|---|---|
| Language | Rust native | C/C++ | C/C++/Rust | C/C++ |
| Runtime | In-process | External | In-process | External |
| Overhead | Low | High (10-50x) | Medium (2x) | Medium |
| Variable Names | โ | โ | โ | โ |
| Source Location | โ | โ | โ | โ |
| Leak Detection | โ | โ | โ | โ |
| UAF Detection | โ | โ | โ | โ ๏ธ |
| Buffer Overflow | โ ๏ธ | โ | โ | โ |
| Thread Analysis | โ | โ | โ | โ |
| Async Support | โ | โ | โ | โ |
| FFI Tracking | โ | โ ๏ธ | โ ๏ธ | โ ๏ธ |
| HTML Dashboard | โ | โ | โ | โ ๏ธ |
| Production Ready | โ ๏ธ | โ | โ | โ ๏ธ |
When to Use memscope-rs
Good fit:
- Rust projects needing variable-level tracking
- Async/await applications
- Development and debugging
- Understanding memory patterns
- Smart pointer analysis
Consider alternatives:
- Valgrind - Deep memory debugging, mature tooling
- AddressSanitizer - Production-grade UAF/overflow detection
- Heaptrack - C/C++ projects, mature profiler
Limitations
- Buffer overflow detection is pattern-based, not runtime enforcement
- Not a replacement for ASAN/Valgrind in production
- Requires code instrumentation (track! macros)
- Performance overhead varies by use case
- Large dataset analysis may have performance impact (see PR Summary)
Contributing
Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.
License
Licensed under MIT OR Apache-2.0.
Acknowledgments
- Built with โค๏ธ for the Rust community
- Inspired by existing memory tracking tools
- Special thanks to all contributors