Class: Ignis::Memory::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/nvruby/memory/stats.rb

Overview

Thread-safe allocation statistics tracking Provides real-time visibility into memory usage patterns

Class Method Summary collapse

Class Method Details

.allocation_countInteger

Returns Total allocation count since startup.

Returns:

  • (Integer)

    Total allocation count since startup



20
21
22
# File 'lib/nvruby/memory/stats.rb', line 20

def allocation_count
  @allocation_count ||= 0
end

.deallocation_countInteger

Returns Total deallocation count since startup.

Returns:

  • (Integer)

    Total deallocation count since startup



25
26
27
# File 'lib/nvruby/memory/stats.rb', line 25

def deallocation_count
  @deallocation_count ||= 0
end

.peak_allocated_bytesInteger

Returns Peak bytes allocated (high-water mark).

Returns:

  • (Integer)

    Peak bytes allocated (high-water mark)



15
16
17
# File 'lib/nvruby/memory/stats.rb', line 15

def peak_allocated_bytes
  @peak_allocated_bytes ||= 0
end

.record_allocation(bytes) ⇒ void

This method returns an undefined value.

Record an allocation

Parameters:

  • bytes (Integer)


32
33
34
35
36
37
38
# File 'lib/nvruby/memory/stats.rb', line 32

def record_allocation(bytes)
  mutex.synchronize do
    @total_allocated_bytes = (total_allocated_bytes + bytes)
    @peak_allocated_bytes = [@peak_allocated_bytes || 0, @total_allocated_bytes].max
    @allocation_count = (allocation_count + 1)
  end
end

.record_deallocation(bytes) ⇒ void

This method returns an undefined value.

Record a deallocation

Parameters:

  • bytes (Integer)


43
44
45
46
47
48
# File 'lib/nvruby/memory/stats.rb', line 43

def record_deallocation(bytes)
  mutex.synchronize do
    @total_allocated_bytes = [total_allocated_bytes - bytes, 0].max
    @deallocation_count = (deallocation_count + 1)
  end
end

.reset!void

This method returns an undefined value.

Reset all statistics



52
53
54
55
56
57
58
59
# File 'lib/nvruby/memory/stats.rb', line 52

def reset!
  mutex.synchronize do
    @total_allocated_bytes = 0
    @peak_allocated_bytes = 0
    @allocation_count = 0
    @deallocation_count = 0
  end
end

.snapshotHash

Returns Statistics snapshot.

Returns:

  • (Hash)

    Statistics snapshot



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nvruby/memory/stats.rb', line 62

def snapshot
  mutex.synchronize do
    {
      total_allocated_bytes: total_allocated_bytes,
      peak_allocated_bytes: peak_allocated_bytes,
      allocation_count: allocation_count,
      deallocation_count: deallocation_count,
      active_allocations: allocation_count - deallocation_count
    }
  end
end

.to_sString

Returns Human-readable statistics.

Returns:

  • (String)

    Human-readable statistics



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nvruby/memory/stats.rb', line 75

def to_s
  s = snapshot
  format(
    "Memory Stats: %s allocated (peak: %s), %d allocs, %d frees, %d active",
    format_bytes(s[:total_allocated_bytes]),
    format_bytes(s[:peak_allocated_bytes]),
    s[:allocation_count],
    s[:deallocation_count],
    s[:active_allocations]
  )
end

.total_allocated_bytesInteger

Returns Total bytes currently allocated.

Returns:

  • (Integer)

    Total bytes currently allocated



10
11
12
# File 'lib/nvruby/memory/stats.rb', line 10

def total_allocated_bytes
  @total_allocated_bytes ||= 0
end