Struct MemoryUsage

Source
pub struct MemoryUsage {
    pub current_bytes: usize,
    pub peak_bytes: usize,
    pub active_allocations: usize,
    pub total_allocations: usize,
}
Expand description

Memory usage tracking and reporting

Fields§

§current_bytes: usize

Current memory usage in bytes

§peak_bytes: usize

Peak memory usage in bytes

§active_allocations: usize

Number of active allocations

§total_allocations: usize

Total allocations made

Implementations§

Source§

impl MemoryUsage

Source

pub fn new() -> Self

Create a new memory usage tracker

Examples found in repository?
examples/memory_efficient_example.rs (line 277)
273fn demo_memory_usage_tracking() -> Result<()> {
274    println!("\n📈 Memory Usage Tracking Demo");
275    println!("-----------------------------");
276
277    let mut usage = MemoryUsage::new();
278
279    println!("Initial state:");
280    print_memory_usage(&usage);
281
282    // Simulate various allocation patterns
283    println!("\nSimulating allocation patterns...");
284
285    // Large allocation
286    usage.allocate(50 * 1024 * 1024); // 50MB
287    println!("After 50MB allocation:");
288    print_memory_usage(&usage);
289
290    // Multiple small allocations
291    for i in 1..=10 {
292        usage.allocate(1024 * 1024); // 1MB each
293        if i % 3 == 0 {
294            println!("After {} small allocations:", i);
295            print_memory_usage(&usage);
296        }
297    }
298
299    // Peak usage reached
300    println!("Peak memory usage reached:");
301    print_memory_usage(&usage);
302
303    // Simulate deallocations
304    println!("\nSimulating deallocations...");
305    for i in 1..=8 {
306        usage.deallocate(5 * 1024 * 1024); // 5MB each
307        if i % 2 == 0 {
308            println!("After {} deallocations:", i);
309            print_memory_usage(&usage);
310        }
311    }
312
313    println!("\nFinal state (note peak is preserved):");
314    print_memory_usage(&usage);
315
316    Ok(())
317}
Source

pub fn allocate(&mut self, bytes: usize)

Update memory usage statistics

Examples found in repository?
examples/memory_efficient_example.rs (line 286)
273fn demo_memory_usage_tracking() -> Result<()> {
274    println!("\n📈 Memory Usage Tracking Demo");
275    println!("-----------------------------");
276
277    let mut usage = MemoryUsage::new();
278
279    println!("Initial state:");
280    print_memory_usage(&usage);
281
282    // Simulate various allocation patterns
283    println!("\nSimulating allocation patterns...");
284
285    // Large allocation
286    usage.allocate(50 * 1024 * 1024); // 50MB
287    println!("After 50MB allocation:");
288    print_memory_usage(&usage);
289
290    // Multiple small allocations
291    for i in 1..=10 {
292        usage.allocate(1024 * 1024); // 1MB each
293        if i % 3 == 0 {
294            println!("After {} small allocations:", i);
295            print_memory_usage(&usage);
296        }
297    }
298
299    // Peak usage reached
300    println!("Peak memory usage reached:");
301    print_memory_usage(&usage);
302
303    // Simulate deallocations
304    println!("\nSimulating deallocations...");
305    for i in 1..=8 {
306        usage.deallocate(5 * 1024 * 1024); // 5MB each
307        if i % 2 == 0 {
308            println!("After {} deallocations:", i);
309            print_memory_usage(&usage);
310        }
311    }
312
313    println!("\nFinal state (note peak is preserved):");
314    print_memory_usage(&usage);
315
316    Ok(())
317}
Source

pub fn deallocate(&mut self, bytes: usize)

Record memory deallocation

Examples found in repository?
examples/memory_efficient_example.rs (line 306)
273fn demo_memory_usage_tracking() -> Result<()> {
274    println!("\n📈 Memory Usage Tracking Demo");
275    println!("-----------------------------");
276
277    let mut usage = MemoryUsage::new();
278
279    println!("Initial state:");
280    print_memory_usage(&usage);
281
282    // Simulate various allocation patterns
283    println!("\nSimulating allocation patterns...");
284
285    // Large allocation
286    usage.allocate(50 * 1024 * 1024); // 50MB
287    println!("After 50MB allocation:");
288    print_memory_usage(&usage);
289
290    // Multiple small allocations
291    for i in 1..=10 {
292        usage.allocate(1024 * 1024); // 1MB each
293        if i % 3 == 0 {
294            println!("After {} small allocations:", i);
295            print_memory_usage(&usage);
296        }
297    }
298
299    // Peak usage reached
300    println!("Peak memory usage reached:");
301    print_memory_usage(&usage);
302
303    // Simulate deallocations
304    println!("\nSimulating deallocations...");
305    for i in 1..=8 {
306        usage.deallocate(5 * 1024 * 1024); // 5MB each
307        if i % 2 == 0 {
308            println!("After {} deallocations:", i);
309            print_memory_usage(&usage);
310        }
311    }
312
313    println!("\nFinal state (note peak is preserved):");
314    print_memory_usage(&usage);
315
316    Ok(())
317}
Source

pub fn current_mb(&self) -> f64

Get memory usage in MB

Examples found in repository?
examples/memory_efficient_example.rs (line 331)
330fn print_memory_usage(usage: &MemoryUsage) {
331    println!("  Current: {:.2} MB", usage.current_mb());
332    println!("  Peak: {:.2} MB", usage.peak_mb());
333    println!("  Active allocations: {}", usage.active_allocations);
334    println!("  Total allocations: {}", usage.total_allocations);
335}
Source

pub fn peak_mb(&self) -> f64

Get peak memory usage in MB

Examples found in repository?
examples/memory_efficient_example.rs (line 332)
330fn print_memory_usage(usage: &MemoryUsage) {
331    println!("  Current: {:.2} MB", usage.current_mb());
332    println!("  Peak: {:.2} MB", usage.peak_mb());
333    println!("  Active allocations: {}", usage.active_allocations);
334    println!("  Total allocations: {}", usage.total_allocations);
335}

Trait Implementations§

Source§

impl Clone for MemoryUsage

Source§

fn clone(&self) -> MemoryUsage

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MemoryUsage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryUsage

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V