pub struct MemoryTracker { /* private fields */ }Expand description
Core memory tracking functionality.
The MemoryTracker maintains records of all memory allocations and deallocations, provides statistics, and supports exporting data in various formats.
§Performance Characteristics
- DashMap: Lock-free concurrent access for allocations
- Atomic counters: Lock-free statistics updates
- Thread-local: Zero-contention for per-thread tracking
Implementations§
Source§impl MemoryTracker
impl MemoryTracker
Sourcepub fn track_allocation(&self, ptr: usize, size: usize) -> TrackingResult<()>
pub fn track_allocation(&self, ptr: usize, size: usize) -> TrackingResult<()>
Sourcepub fn track_deallocation(&self, ptr: usize) -> TrackingResult<bool>
pub fn track_deallocation(&self, ptr: usize) -> TrackingResult<bool>
Sourcepub fn get_allocation_size(&self, ptr: usize) -> Option<usize>
pub fn get_allocation_size(&self, ptr: usize) -> Option<usize>
Sourcepub fn associate_var(
&self,
ptr: usize,
var_name: String,
type_name: String,
source_file: Option<&str>,
source_line: Option<u32>,
) -> TrackingResult<()>
pub fn associate_var( &self, ptr: usize, var_name: String, type_name: String, source_file: Option<&str>, source_line: Option<u32>, ) -> TrackingResult<()>
Associate a variable name and type with an allocation.
§Arguments
ptr- Memory pointer addressvar_name- Variable nametype_name- Type namesource_file- Source file (optional)source_line- Source line (optional)
Sourcepub fn fast_track_allocation(
&self,
ptr: usize,
size: usize,
var_name: String,
) -> TrackingResult<()>
pub fn fast_track_allocation( &self, ptr: usize, size: usize, var_name: String, ) -> TrackingResult<()>
Fast track allocation with variable name.
§Arguments
ptr- Memory pointer addresssize- Allocation size in bytesvar_name- Variable name
Sourcepub fn get_stats(&self) -> TrackingResult<MemoryStats>
pub fn get_stats(&self) -> TrackingResult<MemoryStats>
Get current memory statistics.
Sourcepub fn detect_leaks(&self) -> (usize, u64)
pub fn detect_leaks(&self) -> (usize, u64)
Detect memory leaks at program shutdown.
This should be called when the program is shutting down to detect allocations that were never freed. Returns the count and total size of allocations that are still active.
Sourcepub fn get_active_allocations(&self) -> TrackingResult<Vec<AllocationInfo>>
pub fn get_active_allocations(&self) -> TrackingResult<Vec<AllocationInfo>>
Get all currently active allocations.
Sourcepub fn get_memory_by_type(&self) -> TrackingResult<HashMap<String, usize>>
pub fn get_memory_by_type(&self) -> TrackingResult<HashMap<String, usize>>
Get memory grouped by type.
Sourcepub fn set_fast_mode(&self, enabled: bool)
pub fn set_fast_mode(&self, enabled: bool)
Enable or disable fast mode.
Sourcepub fn is_fast_mode(&self) -> bool
pub fn is_fast_mode(&self) -> bool
Check if fast mode is enabled.
Sourcepub fn enable_fast_mode(&self)
pub fn enable_fast_mode(&self)
Enable fast mode for testing.
Sourcepub fn ensure_memory_analysis_path<P: AsRef<Path>>(&self, path: P) -> PathBuf
pub fn ensure_memory_analysis_path<P: AsRef<Path>>(&self, path: P) -> PathBuf
Ensure memory analysis path exists and return the full path.
Sourcepub fn ensure_memscope_path<P: AsRef<Path>>(&self, path: P) -> PathBuf
pub fn ensure_memscope_path<P: AsRef<Path>>(&self, path: P) -> PathBuf
Ensure path uses .memscope extension and is in MemoryAnalysis directory.
Sourcepub fn export_to_memscope<P: AsRef<Path>>(&self, path: P) -> TrackingResult<()>
pub fn export_to_memscope<P: AsRef<Path>>(&self, path: P) -> TrackingResult<()>
Export memory tracking data to .memscope file format (JSON content).
This method exports memory tracking data with a .memscope file extension. The content is serialized as JSON for human readability and interoperability.
Sourcepub fn export_to_json<P: AsRef<Path>>(&self, path: P) -> TrackingResult<()>
pub fn export_to_json<P: AsRef<Path>>(&self, path: P) -> TrackingResult<()>
Export memory tracking data to JSON format.
Trait Implementations§
Source§impl Default for MemoryTracker
impl Default for MemoryTracker
Auto Trait Implementations§
impl !Freeze for MemoryTracker
impl !RefUnwindSafe for MemoryTracker
impl Send for MemoryTracker
impl Sync for MemoryTracker
impl Unpin for MemoryTracker
impl UnsafeUnpin for MemoryTracker
impl UnwindSafe for MemoryTracker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more