I was playing with garbage collection in Pry, and noticed that for some reason it doesn't seem to work. I was trying different ways to set my big array as nil
, etc, while breaking my head on the question "why in the world I simply cannot deallocate the freaking memory with GC.start
?"
Then I went to IRB and suddenly it worked! I'm wondering what could cause this, and if you didn't know the answer but found it, I'd also like to know how.
My simple code (I'm on 1.9.3-p327, and beware, this will eat up 1-2 gigs):
a = []
for i in (1..1000000)
a[i] = 'x' * 100
end
here, I observed the memory increase, and then later:
for i in (1..1000000)
a[i] = i
end
and then,
GC.start
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_
:You can alter the default number of previous results saved by calling
Pry.memory_size=
in your~/.pryrc
:or temporarily in a running Pry (will erase all existing history as well):
We can see this working like so: