1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
//! Run-time memory tracking and profiling.
//!
//! ## First steps
//!
//! Add `re_memory` to your `Cargo.toml`:
//!
//! ```toml
//! cargo add re_memory
//! ```
//!
//! Install the [`AccountingAllocator`] in your `main.rs`:
//! ```no_run
//! use re_memory::AccountingAllocator;
//!
//! #[global_allocator]
//! static GLOBAL: AccountingAllocator<std::alloc::System>
//! = AccountingAllocator::new(std::alloc::System);
//! ```
//!
//! ### Checking memory use
//! Use [`MemoryUse::capture`] to get the current memory use of your application.
//!
//! ### Finding memory leaks
//! Turn on memory tracking at the top of your `main()` function:
//!
//! ```rs
//! re_memory::accounting_allocator::set_tracking_callstacks(true);
//! ```
//!
//! Now let your app run for a while, and then call [`accounting_allocator::tracking_stats`]
//! to get the statistics. Any memory leak should show up in
//! [`TrackingStatistics::top_callstacks`].
//!
//! ### More
//! See also [`accounting_allocator`].
pub mod accounting_allocator;
mod allocation_tracker;
mod memory_history;
mod memory_limit;
mod memory_use;
mod ram_warner;
pub mod util;
#[cfg(not(target_arch = "wasm32"))]
mod backtrace_native;
#[cfg(not(target_arch = "wasm32"))]
use backtrace_native::Backtrace;
#[cfg(target_arch = "wasm32")]
mod backtrace_web;
#[cfg(target_arch = "wasm32")]
use backtrace_web::Backtrace;
pub use {
accounting_allocator::{AccountingAllocator, TrackingStatistics},
allocation_tracker::{CallstackStatistics, ReadableBacktrace},
memory_history::MemoryHistory,
memory_limit::MemoryLimit,
memory_use::MemoryUse,
ram_warner::*,
};
/// Number of allocation and their total size.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct CountAndSize {
/// Number of allocations.
pub count: usize,
/// Number of bytes.
pub size: usize,
}
impl CountAndSize {
pub const ZERO: Self = Self { count: 0, size: 0 };
/// Add an allocation.
#[inline]
pub fn add(&mut self, size: usize) {
self.count += 1;
self.size += size;
}
/// Remove an allocation.
#[inline]
pub fn sub(&mut self, size: usize) {
self.count -= 1;
self.size -= size;
}
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
struct BacktraceHash(u64);
impl BacktraceHash {
pub fn new(backtrace: &Backtrace) -> Self {
use std::hash::{Hash as _, Hasher as _};
let mut hasher =
std::hash::BuildHasher::build_hasher(&ahash::RandomState::with_seeds(0, 1, 2, 3));
backtrace.hash(&mut hasher);
Self(hasher.finish())
}
}
impl nohash_hasher::IsEnabled for BacktraceHash {}