Class: DSAVisualizer::MemoryTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/dsa_visualizer/memory_tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemoryTracker

Returns a new instance of MemoryTracker.



5
6
7
8
9
# File 'lib/dsa_visualizer/memory_tracker.rb', line 5

def initialize
  @allocations = []
  @operations = []
  @step_count = 0
end

Instance Attribute Details

#allocationsObject (readonly)

Returns the value of attribute allocations.



3
4
5
# File 'lib/dsa_visualizer/memory_tracker.rb', line 3

def allocations
  @allocations
end

#operationsObject (readonly)

Returns the value of attribute operations.



3
4
5
# File 'lib/dsa_visualizer/memory_tracker.rb', line 3

def operations
  @operations
end

Instance Method Details



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dsa_visualizer/memory_tracker.rb', line 30

def print_summary
  puts "\nšŸ“Š Memory & Operations Summary:".colorize(:cyan).bold
  puts "\nAllocations: #{@allocations.size}"
  @allocations.each do |alloc|
    puts "  - #{alloc[:type]} (size: #{alloc[:size]}) at 0x#{alloc[:address].to_s(16)}"
  end

  puts "\nOperations: #{@operations.size}"
  @operations.each do |op|
    puts "  #{op[:step]}. #{op[:operation]}: #{op[:details]}"
  end
end

#resetObject



43
44
45
46
47
# File 'lib/dsa_visualizer/memory_tracker.rb', line 43

def reset
  @allocations.clear
  @operations.clear
  @step_count = 0
end

#track_allocation(type, size, object) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/dsa_visualizer/memory_tracker.rb', line 11

def track_allocation(type, size, object)
  @allocations << {
    type: type,
    size: size,
    address: object.object_id,
    timestamp: Time.now
  }
end

#track_operation(operation, details) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/dsa_visualizer/memory_tracker.rb', line 20

def track_operation(operation, details)
  @step_count += 1
  @operations << {
    step: @step_count,
    operation: operation,
    details: details,
    timestamp: Time.now
  }
end