-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path007_iter_1.rb
More file actions
44 lines (32 loc) · 825 Bytes
/
Copy path007_iter_1.rb
File metadata and controls
44 lines (32 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Thing; end
list = Array.new(1000) { Thing.new }
puts ObjectSpace.each_object(Thing).count
list.each.with_index do |item, i|
GC.start
puts ObjectSpace.each_object(Thing).count if i == 500
end
list = nil
GC.start
puts ObjectSpace.each_object(Thing).count
puts 'With shift deallocation'
list = Array.new(1000) { Thing.new }
puts ObjectSpace.each_object(Thing).count
while list.count > 0
GC.start
puts ObjectSpace.each_object(Thing).count if list.count == 500
item = list.shift
end
GC.start
puts ObjectSpace.each_object(Thing).count
# each! Pattern
puts 'With each! pattern'
class Array
def each!
while count > 0
yield shift # Shift 1 item and execute block on it without &block capturing
end
end
end
Array.new(1000).each { |e| e }
GC.start
puts ObjectSpace.each_object(Thing).count