This is because Pry stores the output of the last 100 commands by default. As a result, your object is still referenced and will not be garbage collected until enough commands have been run to push it out of Pry’s output history.
You should be able to find your object in the current Pry instance’s output history using _out_
:
_out_.to_a
You can alter the default number of previous results saved by calling Pry.memory_size=
in your ~/.pryrc
:
Pry.memory_size = 1
or temporarily in a running Pry (will erase all existing history as well):
_pry_.memory_size = 1
We can see this working like so:
$ pry
_pry_.memory_size = 100 # default
class C; end
C.new
ObjectSpace.each_object.grep(C).count #=> 1
GC.start
ObjectSpace.each_object.grep(C).count #=> 1
$ pry
_pry_.memory_size = 0
class C; end
C.new
ObjectSpace.each_object.grep(C).count #=> 1
GC.start
ObjectSpace.each_object.grep(C).count #=> 0