Crates.io | retry_alloc |
lib.rs | retry_alloc |
version | 0.1.0 |
source | src |
created_at | 2024-09-03 17:26:15.487496 |
updated_at | 2024-09-03 17:26:15.487496 |
description | A global allocator wrapper that will retry failed allocations. |
homepage | |
repository | https://github.com/xTachyon/retry_alloc |
max_upload_size | |
id | 1361875 |
size | 18,374 |
A global allocator wrapper that will retry allocations a few times if one allocation fails.
The assumption is that when one allocation fails, it is because the system is out of memory. In that case, the following things might happen:
In all the cases, some memory gets freed on the system. This means that retrying an allocation after waiting some time might succeed, at the expense of a latency spike in the application, which is usually a better outcome than the application dying.
An allocation failure is still possible, but this makes it more unlikely.
Example
#[global_allocator]
static GLOBAL: RetryAlloc = RetryAlloc::new(System);
With stats_alloc
:
static RETRY: RetryAlloc = RetryAlloc::new(System);
#[global_allocator]
static GLOBAL: StatsAlloc<&RetryAlloc> = StatsAlloc::new(&RETRY);
In this case, RETRY
has to be a separate static because StatsAlloc
doesn't have a way to access the inner alloc in order to retrieve fail statistics (if you need that), and having it in the inverse order might result in some stats being counted wrong due to StatsAlloc
still counting if the allocation failed.