class
Sepia::MemoryLimiter
- Sepia::MemoryLimiter
- Reference
- Object
Overview
Memory pressure detection and management system.
Monitors system memory usage and provides signals for when caches should be purged or memory-saving measures should be taken. Works across different platforms (Linux, macOS, Windows).
Example
limiter = MemoryLimiter.new(
warning_threshold: "70%",
critical_threshold: "85%",
check_interval: 30.seconds
)
limiter.on_warning { puts "Memory usage is high" }
limiter.on_critical { cache.clear }
limiter.start_monitoring
Defined in:
sepia/memory_limiter.crConstructors
-
.new(warning_threshold : Float64 = 0.7, critical_threshold : Float64 = 0.85, emergency_threshold : Float64 = 0.95, check_interval : Time::Span = 30.seconds)
Creates a new memory limiter.
Instance Method Summary
-
#check_interval : Time::Span
Monitoring interval
-
#check_now : PressureLevel
Forces an immediate memory check.
-
#critical? : Bool
Checks if memory pressure is at critical level or higher.
- #critical_threshold : Float64
-
#current_pressure : PressureLevel
Gets the current memory pressure level.
-
#current_stats : MemoryStats
Current memory statistics
-
#emergency? : Bool
Checks if memory pressure is at emergency level.
- #emergency_threshold : Float64
-
#monitoring : Bool
Monitoring state
- #on_critical : Proc(Nil) | Nil
- #on_critical=(on_critical : Proc(Nil) | Nil)
- #on_emergency : Proc(Nil) | Nil
- #on_emergency=(on_emergency : Proc(Nil) | Nil)
- #on_normal : Proc(Nil) | Nil
- #on_normal=(on_normal : Proc(Nil) | Nil)
-
#on_warning : Proc(Nil) | Nil
Event callbacks
-
#on_warning=(on_warning : Proc(Nil) | Nil)
Event callbacks
-
#start_monitoring : Void
Starts monitoring memory usage in the background.
-
#status_description : String
Gets a human-readable description of current memory state.
-
#stop_monitoring : Void
Stops monitoring memory usage.
-
#suggest_cache_size(max_size : Int32) : Int32
Suggests cache size based on current memory pressure.
-
#warning? : Bool
Checks if memory pressure is at warning level or higher.
-
#warning_threshold : Float64
Memory usage thresholds
Constructor Detail
Creates a new memory limiter.
Parameters
- warning_threshold : Memory usage percentage for warning (default: 0.7 = 70%)
- critical_threshold : Memory usage percentage for critical (default: 0.85 = 85%)
- emergency_threshold : Memory usage percentage for emergency (default: 0.95 = 95%)
- check_interval : How often to check memory usage (default: 30 seconds)
Example
limiter = MemoryLimiter.new(
warning_threshold: 0.75,
critical_threshold: 0.90,
check_interval: 60.seconds
)
Instance Method Detail
Forces an immediate memory check.
Updates current statistics and triggers appropriate callbacks.
Returns
Current memory pressure level.
Example
pressure = limiter.check_now
puts "Current pressure: #{pressure}"
Checks if memory pressure is at critical level or higher.
Returns
true if usage >= critical_threshold, false otherwise.
Example
if limiter.critical?
cache.clear
GC.collect
end
Gets the current memory pressure level.
Returns
Current pressure level without forcing a new check.
Example
pressure = limiter.current_pressure
case pressure
when .warning?
puts "Memory usage is high"
when .critical?
puts "Memory usage is critical"
end
Checks if memory pressure is at emergency level.
Returns
true if usage >= emergency_threshold, false otherwise.
Example
if limiter.emergency?
raise "Out of memory!"
end
Starts monitoring memory usage in the background.
Creates a fiber that periodically checks memory usage and triggers callbacks based on threshold levels.
Example
limiter.start_monitoring
Gets a human-readable description of current memory state.
Returns
Description string.
Example
puts limiter.status_description
# => "Memory usage: 2.3 GB of 8.0 GB (28.8%) - Normal"
Suggests cache size based on current memory pressure.
Parameters
- max_size : Maximum desired cache size
Returns
Recommended cache size based on memory pressure.
Example
recommended = limiter.suggest_cache_size(1000)
cache.resize(recommended)
Checks if memory pressure is at warning level or higher.
Returns
true if usage >= warning_threshold, false otherwise.
Example
if limiter.warning?
cache.cleanup
end