M
Michael Neumann
Hi. Take a look at the source code at the end of the email and run it.
The expected output for me is, either an empty string ("") or nothing
(and this up to 1000 times). But, the output I get (on both Ruby 1.8.x
and 1.9) is like that shown below:
"w.rb:16:in `_id2ref'"
["w.rb:16:in `_id2ref'", "w.rb:16:in `each_object'", "w.rb:14:in
`each'", "/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each_key'",
"/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each'", "w.rb:14:in
`each_object'", "w.rb:30"]
"w.rb:16:in `_id2ref'"
"/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each'"
"/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each_key'"
"w.rb:14:in `each'"
#<RangeError: 0x4086550 is recycled object>
["w.rb:16:in `_id2ref'", "w.rb:16:in `each_object'", "w.rb:14:in
`each'", "/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each_key'",
"/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each'", "w.rb:14:in
`each_object'", "w.rb:30"]
#<RangeError: 0x4084e1c is recycled object>
"w.rb:14:in `each'"
"w.rb:14:in `each_object'"
"w.rb:14:in `each'"
"w.rb:16:in `_id2ref'"
"w.rb:14:in `each_object'"
"w.rb:30"
There must be something wrong, or is this correct? Does Ruby reuse
object_id's? BTW, what happens if an application uses more than 4 billon
(32-bit) objects? Does Ruby fail or reuse old object_ids?
Thanks in advance.
Regards,
Michael
#################################
# here the code. if no output is shown, try to
# increase the number of objects registered (1000)
#
require 'set'
class StateRegistry
def initialize
@registered_objects = Set.new
end
def register(obj)
@registered_objects.add(obj.object_id)
end
def each_object
@registered_objects.each do |oid|
begin
obj = ObjectSpace._id2ref(oid)
rescue RangeError
next
end
yield obj
end
end
end
s = StateRegistry.new
1000.times do
s.register("")
end
ObjectSpace.garbage_collect
s.each_object {|obj| p obj}
The expected output for me is, either an empty string ("") or nothing
(and this up to 1000 times). But, the output I get (on both Ruby 1.8.x
and 1.9) is like that shown below:
"w.rb:16:in `_id2ref'"
["w.rb:16:in `_id2ref'", "w.rb:16:in `each_object'", "w.rb:14:in
`each'", "/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each_key'",
"/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each'", "w.rb:14:in
`each_object'", "w.rb:30"]
"w.rb:16:in `_id2ref'"
"/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each'"
"/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each_key'"
"w.rb:14:in `each'"
#<RangeError: 0x4086550 is recycled object>
["w.rb:16:in `_id2ref'", "w.rb:16:in `each_object'", "w.rb:14:in
`each'", "/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each_key'",
"/home/mneumann/usr/lib/ruby/1.9/set.rb:193:in `each'", "w.rb:14:in
`each_object'", "w.rb:30"]
#<RangeError: 0x4084e1c is recycled object>
"w.rb:14:in `each'"
"w.rb:14:in `each_object'"
"w.rb:14:in `each'"
"w.rb:16:in `_id2ref'"
"w.rb:14:in `each_object'"
"w.rb:30"
There must be something wrong, or is this correct? Does Ruby reuse
object_id's? BTW, what happens if an application uses more than 4 billon
(32-bit) objects? Does Ruby fail or reuse old object_ids?
Thanks in advance.
Regards,
Michael
#################################
# here the code. if no output is shown, try to
# increase the number of objects registered (1000)
#
require 'set'
class StateRegistry
def initialize
@registered_objects = Set.new
end
def register(obj)
@registered_objects.add(obj.object_id)
end
def each_object
@registered_objects.each do |oid|
begin
obj = ObjectSpace._id2ref(oid)
rescue RangeError
next
end
yield obj
end
end
end
s = StateRegistry.new
1000.times do
s.register("")
end
ObjectSpace.garbage_collect
s.each_object {|obj| p obj}