Skip to content

Instantly share code, notes, and snippets.

@byroot
Created April 25, 2025 09:33
Show Gist options
  • Save byroot/dcbe4db5c7968fcf9a40b918496b081d to your computer and use it in GitHub Desktop.
Save byroot/dcbe4db5c7968fcf9a40b918496b081d to your computer and use it in GitHub Desktop.
# require "objspace"
# puts ObjectSpace.dump(Object.new)
# puts ObjectSpace.dump(Object.new.tap(&:object_id))
# puts ObjectSpace.dump_all(since: 1, output: :stdout)
o = Object.new
p [o.object_id, o.object_id]
o = Marshal.load(Marshal.dump(o))
p [o.object_id, o.object_id]
ObjectSpace._id2ref(8)
o = o.dup
p [o.object_id, o.object_id]
o = o.clone
p [o.object_id, o.object_id]
o = Hash.new
p [o.object_id, o.object_id]
o = Marshal.load(Marshal.dump(o))
p [o.object_id, o.object_id]
o = Class.new
p [o.object_id, o.object_id]
class TooComplex
end
20.times do |i|
TooComplex.new.instance_variable_set("@a#{i}", 1)
end
o = TooComplex.new
o.instance_variable_set("@b", 1)
p [o.object_id, o.object_id]
o = Marshal.load(Marshal.dump(o))
p [o.object_id, o.object_id]
o = TooComplex.new
id = o.object_id
o.instance_variable_set("@b", 1)
p [id, o.object_id]
GC.start
def fin
->(id) { p [:fin, id] }
end
o = Object.new
p [o.object_id, o.object_id]
ObjectSpace.define_finalizer(o, fin)
o = nil
GC.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment