Skip to main content

MemoryPressure

Struct MemoryPressure 

Source
pub struct MemoryPressure { /* private fields */ }
Expand description

A memory-pressure guard that holds size_bytes of allocated memory for its entire lifetime.

Drop the guard to release the memory.

§Example

use dev_chaos::memory_pressure::MemoryPressure;

{
    let _hold = MemoryPressure::allocate(1_024 * 1_024); // 1 MiB
    // ... run code under memory pressure ...
} // memory released here

Implementations§

Source§

impl MemoryPressure

Source

pub fn allocate(size_bytes: usize) -> Self

Allocate and hold size_bytes of zeroed memory.

Always succeeds for sizes the allocator can satisfy. Returns the guard; drop it to release the memory.

For testing OOM / allocation-failure paths specifically, see MemoryPressure::try_allocate.

Source

pub fn try_allocate(size_bytes: usize) -> Result<Self, TryReserveError>

Attempt to allocate size_bytes, returning the error path rather than panicking on allocator failure.

This catches try_reserve_exact errors. For genuinely OOM scenarios on most platforms the kernel may kill the process before this returns; honest behavior is platform-specific.

Source

pub fn size_bytes(&self) -> usize

The number of bytes this guard is holding.

Source

pub fn allocate_kib(kib: usize) -> Self

Convenience: allocate kib KiB.

Source

pub fn allocate_mib(mib: usize) -> Self

Convenience: allocate mib MiB.

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> 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, 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.