Ruby内存管理 [英] Ruby Memory Management

查看:247
本文介绍了Ruby内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Ruby一段时间,我发现,对于更大的项目,它可以占用相当大的内存。




  • 请让每个答案都有一个最佳实践,让社群投票
  • 不要这样做:



      def方法(x)
    x.split(不管args是什么)
    end

    或:

      def方法(x)
    x.gsub(无论参数是什么)
    end

    这两者都将永久性地泄露ruby 1.8的内存。 5和1.8.6 。 (不知道1.8.7,因为我没有尝试过,但我真的希望它是固定的。)解决方法是愚蠢的,涉及创建一个局部变量。你不必使用本地,只需创建一个...



    这就是为什么我对ruby语言有很多的爱,但不尊重MRI


    I have been using Ruby for a while now and I find, for bigger projects, it can take up a fair amount of memory. What are some best practices for reducing memory usage in Ruby?

    • Please, let each answer have one "best practice" and let the community vote it up.

    解决方案

    Don't do this:

    def method(x)
      x.split( doesn't matter what the args are )
    end
    

    or this:

    def method(x)
      x.gsub( doesn't matter what the args are )
    end
    

    Both will permanently leak memory in ruby 1.8.5 and 1.8.6. (not sure about 1.8.7 as I haven't tried it, but I really hope it's fixed.) The workaround is stupid and involves creating a local variable. You don't have to use the local, just create one...

    Things like this are why I have lots of love for the ruby language, but no respect for MRI

    这篇关于Ruby内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆