class

Sepia::MemoryLimiter

Inherits Reference < Object

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

Constructors

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.

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
)
Source

Instance methods

check_interval

Monitoring interval

Source
check_now

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}"
Source
critical?

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
Source
critical_threshold
Source
current_pressure

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
Source
current_stats

Current memory statistics

Source
emergency?

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
Source
emergency_threshold
Source
monitoring

Monitoring state ameba:disable Naming/QueryBoolMethods

Source
on_critical
Source
on_critical=(on_critical : Proc(Nil) | Nil)
Source
on_emergency
Source
on_emergency=(on_emergency : Proc(Nil) | Nil)
Source
on_normal
Source
on_normal=(on_normal : Proc(Nil) | Nil)
Source
on_warning

Event callbacks

Source
on_warning=(on_warning : Proc(Nil) | Nil)

Event callbacks

Source
start_monitoring

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
Source
status_description

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"
Source
stop_monitoring

Stops monitoring memory usage.

Example

limiter.stop_monitoring
Source
suggest_cache_size(max_size : Int32) : Int32

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)
Source
warning?

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
Source
warning_threshold

Memory usage thresholds

Source

Nested types