A memory-safe wrapper for heavy iterations that monitors RAM usage and triggers an interactive failsafe if the environment approaches critical capacity.
Details
The function provides a safeguard against memory-related crashes during large-scale batch processing:
Iterates through the `items` vector, applying `target_func` to each element.
Every 50 iterations, it checks the current memory usage using `gc()`.
If the memory usage exceeds `limit_mb`, it first attempts a deep garbage collection to recover RAM.
If memory remains above the threshold after GC, it triggers an interactive alarm, allowing the user to:
Save current progress to `save_path` and abort.
Ignore the limit and attempt to continue.
Abort immediately without saving.
Warning
This function modifies files on disk or the global environment. Please ensure you have a backup or are using version control (e.g., Git) before execution.
Examples
if (interactive()) {
loop_guardian(items = 1:5, target_func = print)
}
