简体   繁体   English

Ruby / Rails内存使用情况

[英]Ruby/Rails memory usage

Hey guys, in the following code: 嗨,在下面的代码中:

  def process(batch_size=1000)
     claim_numbers.each_slice(batch_size) do |numbers_batch|
       claims = Claim.find(:all, :conditions => ["claim_number in (?)", numbers_batch])
       # do something with claims
     end
   end

In one of my Rails models I'm processing a lot of claim_numbers , I'm simulating a find_in_batches method in order to do not load a lot of records into memory, then my question is : in terms of memory, what happens with the claims variable in each iteration? 在我的一个Rails模型中,我正在处理很多claim_numbers ,为了不将很多记录加载到内存中,我正在模拟find_in_batches方法, 那么我的问题是 :就内存而言, claims会发生什么每次迭代中都有变量? when does the Ruby's GC release that portion of memory? Ruby的GC什么时候释放那部分内存?

Any help and tips would be appreciated, thanks in advance! 任何帮助和提示将不胜感激,在此先感谢!

Update: Using Ruby 1.8.7-p72 更新:使用Ruby 1.8.7-p72

Ruby will release the memory as soon as the GC runs. 一旦GC运行,Ruby将释放内存。 Since claims is scoped inside the each_slice block, claim will have no reference outside of the block and when claim is reassigned (because of a next iteration), the previously assigned objects become unreferenced. 由于声明的作用域在each_slice块内部,因此声明在该块外部将没有引用,并且当重新分配声明时(由于下一次迭代),先前分配的对象将变为未引用。 The memory of each object is retained until the GC kicks in. How often the GC runs can be specified with some environment variables (more info at http://blog.evanweaver.com/articles/2009/04/09/ruby-gc-tuning/ ). 将保留每个对象的内存,直到GC启动为止。可以使用一些环境变量来指定GC运行的频率(有关更多信息,请访问http://blog.evanweaver.com/articles/2009/04/09/ruby-gc -tuning / )。

If for some reason you retain the objects (because there is still a reference to it, eg you put the objects in an array or hash), the memory for that object is not released. 如果出于某种原因保留了对象(因为仍然有对它的引用,例如,将对象放入数组或哈希中),则不会释放该对象的内存。 If you monitor your app, you can see an increased memory usage, but also increasing CPU usage, since ruby's GC is non-generational, which means that it goes over all objects, every time, to see if they can be collected. 如果监视您的应用程序,则可以看到内存使用率增加,但CPU使用率也增加,因为ruby的GC是非世代的,这意味着它每次都会遍历所有对象以查看是否可以收集它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM