class Sepia::MemoryLimiter

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

Constructors

Instance Method Summary

Constructor Detail

def self.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
)

[View source]

Instance Method Detail

def check_interval : Time::Span #

Monitoring interval


[View source]
def check_now : PressureLevel #

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}"

[View source]
def critical? : Bool #

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

[View source]
def critical_threshold : Float64 #

[View source]
def current_pressure : PressureLevel #

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

[View source]
def current_stats : MemoryStats #

Current memory statistics


[View source]
def emergency? : Bool #

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

[View source]
def emergency_threshold : Float64 #

[View source]
def monitoring : Bool #

Monitoring state


[View source]
def on_critical : Proc(Nil) | Nil #

[View source]
def on_critical=(on_critical : Proc(Nil) | Nil) #

[View source]
def on_emergency : Proc(Nil) | Nil #

[View source]
def on_emergency=(on_emergency : Proc(Nil) | Nil) #

[View source]
def on_normal : Proc(Nil) | Nil #

[View source]
def on_normal=(on_normal : Proc(Nil) | Nil) #

[View source]
def on_warning : Proc(Nil) | Nil #

Event callbacks


[View source]
def on_warning=(on_warning : Proc(Nil) | Nil) #

Event callbacks


[View source]
def start_monitoring : Void #

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

[View source]
def status_description : String #

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"

[View source]
def stop_monitoring : Void #

Stops monitoring memory usage.

Example

limiter.stop_monitoring

[View source]
def 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)

[View source]
def warning? : Bool #

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

[View source]
def warning_threshold : Float64 #

Memory usage thresholds


[View source]