GC.start在Pry中无效,但在IRB中有效 [英] GC.start has no effect in Pry, but does in IRB
问题描述
我在Pry中玩垃圾收集,并注意到由于某种原因它似乎不起作用.我正在尝试不同的方式将大数组设置为nil
等,同时打破了这个问题:为什么我根本无法使用GC.start
释放异常内存?"
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
?"
然后我去了IRB,突然工作了!我想知道是什么原因引起的,如果您不知道答案却找到了答案,我也想知道如何解决.
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.
我的简单代码(我在1.9.3-p327上,请注意,这将消耗1-2个演出):
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
然后
GC.start
推荐答案
这是因为.因此,您的对象仍会被引用,除非运行了足够的命令将其从Pry的输出历史记录中推出,否则不会对其进行垃圾收集.
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.
您应该能够使用 _out_
:
You should be able to find your object in the current Pry instance’s output history using _out_
:
_out_.to_a
您可以通过调用 Pry.memory_size=
在您的~/.pryrc
:
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
这篇关于GC.start在Pry中无效,但在IRB中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!