Ruby Memory Management

后端 未结 5 1832
遥遥无期
遥遥无期 2020-12-23 21:32

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 Ru

5条回答
  •  有刺的猬
    2020-12-23 22:25

    Don't abuse symbols.

    Each time you create a symbol, ruby puts an entry in it's symbol table. The symbol table is a global hash which never gets emptied.
    This is not technically a memory leak, but it behaves like one. Symbols don't take up much memory so you don't need to be too paranoid, but it pays to be aware of this.

    A general guideline: If you've actually typed the symbol in code, it's fine (you only have a finite amount of code after all), but don't call to_sym on dynamically generated or user-input strings, as this opens the door to a potentially ever-increasing number

提交回复
热议问题