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
impl MemoryUsage
Sourcepub fn new() -> Self
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}
Sourcepub fn allocate(&mut self, bytes: usize)
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}
Sourcepub fn deallocate(&mut self, bytes: usize)
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}
Trait Implementations§
Source§impl Clone for MemoryUsage
impl Clone for MemoryUsage
Source§fn clone(&self) -> MemoryUsage
fn clone(&self) -> MemoryUsage
Returns a duplicate of the value. Read more
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for MemoryUsage
impl Debug for MemoryUsage
Auto Trait Implementations§
impl Freeze for MemoryUsage
impl RefUnwindSafe for MemoryUsage
impl Send for MemoryUsage
impl Sync for MemoryUsage
impl Unpin for MemoryUsage
impl UnwindSafe for MemoryUsage
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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 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>
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