Class: DSAVisualizer::MemoryTracker
- Inherits:
-
Object
- Object
- DSAVisualizer::MemoryTracker
- Defined in:
- lib/dsa_visualizer/memory_tracker.rb
Instance Attribute Summary collapse
-
#allocations ⇒ Object
readonly
Returns the value of attribute allocations.
-
#operations ⇒ Object
readonly
Returns the value of attribute operations.
Instance Method Summary collapse
-
#initialize ⇒ MemoryTracker
constructor
A new instance of MemoryTracker.
- #print_summary ⇒ Object
- #reset ⇒ Object
- #track_allocation(type, size, object) ⇒ Object
- #track_operation(operation, details) ⇒ Object
Constructor Details
#initialize ⇒ MemoryTracker
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
#allocations ⇒ Object (readonly)
Returns the value of attribute allocations.
3 4 5 |
# File 'lib/dsa_visualizer/memory_tracker.rb', line 3 def allocations @allocations end |
#operations ⇒ Object (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
#print_summary ⇒ Object
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 |
#reset ⇒ Object
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 |