Class: AiRootShield::Enterprise::MemoryManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_root_shield/enterprise/performance_optimizer.rb

Overview

Memory management and optimization

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MemoryManager

Returns a new instance of MemoryManager.



502
503
504
505
506
507
508
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 502

def initialize(config)
  @config = config
  @stats = init_stats
  @last_cleanup_size = 0
  @cache_hit_rate = 0.0
  @caches = {}
end

Instance Attribute Details

#cache_hit_rate ⇒ Object (readonly)

Returns the value of attribute cache_hit_rate.



500
501
502
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 500

def cache_hit_rate
  @cache_hit_rate
end

#last_cleanup_size ⇒ Object (readonly)

Returns the value of attribute last_cleanup_size.



500
501
502
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 500

def last_cleanup_size
  @last_cleanup_size
end

#stats ⇒ Object (readonly)

Returns the value of attribute stats.



500
501
502
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 500

def stats
  @stats
end

Instance Method Details

#cleanup ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 510

def cleanup
  start_size = current_usage
  
  # Force garbage collection
  GC.start
  
  # Clean up caches
  cleanup_caches
  
  end_size = current_usage
  @last_cleanup_size = start_size - end_size
  
  update_stats
end

#current_usage ⇒ Object



535
536
537
538
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 535

def current_usage
  # Simplified memory usage calculation
  ObjectSpace.count_objects[:TOTAL] * 40 # Rough estimate
end

#memory_usage_ratio ⇒ Object



540
541
542
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 540

def memory_usage_ratio
  current_usage.to_f / @config[:max_cache_size]
end

#optimize_caches ⇒ Object



525
526
527
528
529
530
531
532
533
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 525

def optimize_caches
  @caches.each do |name, cache|
    if cache.size > (@config[:max_cache_size] / @caches.length)
      # Remove oldest 20% of entries
      remove_count = (cache.size * 0.2).to_i
      cache.shift(remove_count)
    end
  end
end

#reconfigure(optimization_level) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/ai_root_shield/enterprise/performance_optimizer.rb', line 544

def reconfigure(optimization_level)
  case optimization_level
  when :conservative
    @config[:max_cache_size] = 50_000_000 # 50MB
  when :balanced
    @config[:max_cache_size] = 100_000_000 # 100MB
  when :aggressive
    @config[:max_cache_size] = 200_000_000 # 200MB
  when :maximum
    @config[:max_cache_size] = 500_000_000 # 500MB
  end
end