Module: Lich::Util::MemoryReleaser

Defined in:
documented/util/memoryreleaser.rb

Overview

Memory management module that provides automatic and manual memory release functionality for Ruby applications. Supports Linux, macOS, and Windows platforms.

This module uses a singleton pattern to manage a background thread that periodically releases memory back to the operating system after running Ruby's garbage collector.

Settings are persisted per-character using InstanceSettings and include:

  • auto_start: automatically start the memory releaser on module load
  • interval: time in seconds between memory releases
  • verbose: enable detailed logging

Examples:

Enable auto-start

MemoryReleaser.auto_start!

Basic usage

MemoryReleaser.start
# ... application runs ...
MemoryReleaser.stop

With custom interval and verbose logging

MemoryReleaser.start(interval: 600, verbose: true)

Change settings

MemoryReleaser.interval!(1200)  # Change to 20 minutes
MemoryReleaser.verbose!(true)   # Enable verbose logging

Manual memory release

MemoryReleaser.release

Check status

status = MemoryReleaser.status
puts "Running: #{status[:running]}"
puts "Auto-start: #{status[:auto_start]}"
puts "Platform: #{status[:platform]}"

Run benchmark

MemoryReleaser.benchmark

Defined Under Namespace

Classes: Manager

Constant Summary collapse

DEFAULT_SETTINGS =

Default settings for memory releaser

{
  auto_start: true, # Disabled by default, user must enable
  interval: 900, # default of 15 minutes
  verbose: false,
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.command_queueQueue (readonly)

Returns the command queue for communicating with the launcher thread.

Returns:

  • (Queue)

    the command queue for communicating with the launcher thread



802
803
804
# File 'documented/util/memoryreleaser.rb', line 802

def command_queue
  @command_queue
end

.worker_threadThread? (readonly)

Returns the current worker thread.

Returns:

  • (Thread, nil)

    the current worker thread



807
808
809
# File 'documented/util/memoryreleaser.rb', line 807

def worker_thread
  @worker_thread
end

Class Method Details

.auto_disable!void

This method returns an undefined value.

Disable auto-start and stop the memory releaser



860
861
862
# File 'documented/util/memoryreleaser.rb', line 860

def auto_disable!
  instance.auto_disable!
end

.auto_start!Thread?

Enable auto-start and start the memory releaser

Returns:

  • (Thread, nil)

    the background thread, or nil if failed to start

See Also:



852
853
854
# File 'documented/util/memoryreleaser.rb', line 852

def auto_start!
  instance.auto_start!
end

.benchmarkvoid

This method returns an undefined value.

Run a memory release benchmark



910
911
912
# File 'documented/util/memoryreleaser.rb', line 910

def benchmark
  instance.benchmark
end

.instanceManager

Get or create the singleton Manager instance

If auto_start is enabled in settings, automatically starts the memory releaser.

Returns:

  • (Manager)

    the singleton manager instance



814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'documented/util/memoryreleaser.rb', line 814

def instance
  @mutex ||= Mutex.new
  @mutex.synchronize {
    @instance ||= begin
      manager = Manager.new

      # Auto-start if enabled in settings
      if manager.settings[:auto_start]
        manager.start
      end

      manager
    end
  }
end

.interval!(seconds) ⇒ Integer

Update the interval setting

Parameters:

  • seconds (Integer)

    the new interval in seconds

Returns:

  • (Integer)

    the new interval value

See Also:



869
870
871
# File 'documented/util/memoryreleaser.rb', line 869

def interval!(seconds)
  instance.interval!(seconds)
end

.releasevoid

This method returns an undefined value.

Perform a manual memory release



886
887
888
# File 'documented/util/memoryreleaser.rb', line 886

def release
  instance.release
end

.reset!void

This method returns an undefined value.

Reset the singleton instance

Stops the current instance if running and clears the singleton. Useful for testing or when you need to recreate the instance.



920
921
922
923
# File 'documented/util/memoryreleaser.rb', line 920

def reset!
  @instance&.stop
  @instance = nil
end

.running?Boolean

Check if the background thread is running

Returns:

  • (Boolean)

    true if running, false otherwise

See Also:



894
895
896
# File 'documented/util/memoryreleaser.rb', line 894

def running?
  instance.running?
end

.start(interval: nil, verbose: nil) ⇒ Thread?

Start the background memory release thread

Parameters:

  • interval (Integer, nil) (defaults to: nil)

    time in seconds between memory releases (default: uses saved setting)

  • verbose (Boolean, nil) (defaults to: nil)

    whether to enable verbose logging (default: uses saved setting)

Returns:

  • (Thread, nil)

    the background thread, or nil if failed to start

See Also:



836
837
838
# File 'documented/util/memoryreleaser.rb', line 836

def start(interval: nil, verbose: nil)
  instance.start(interval: interval, verbose: verbose)
end

.statusHash

Get the current status

Returns:

  • (Hash)

    status information

See Also:



902
903
904
# File 'documented/util/memoryreleaser.rb', line 902

def status
  instance.status
end

.stopvoid

This method returns an undefined value.

Stop the background memory release thread



844
845
846
# File 'documented/util/memoryreleaser.rb', line 844

def stop
  instance.stop
end

.verbose!(enabled) ⇒ Boolean

Update the verbose setting

Parameters:

  • enabled (Boolean)

    whether to enable verbose logging

Returns:

  • (Boolean)

    the new verbose value

See Also:



878
879
880
# File 'documented/util/memoryreleaser.rb', line 878

def verbose!(enabled)
  instance.verbose!(enabled)
end