Ruby企業版(REE)調優

 本篇文章如果有訂閱Ruby5的童鞋們應該知道的。

最近國外的一個同行Bryan Liles做過一個對RubyEE下執行測試的的評測:

未調優前:

410 scenarios (410 passed)

3213 steps (3213 passed) 

9m29.685s

 調優後:

410 scenarios (410 passed) 

3213 steps (3213 passed) 

5m58.661s

差距怎麼這麼大呢?

可以去看看REE官方文檔關於GC性能調整的章節。只需要設置5個參數,我們也可以得到上面的效果:

export RUBY_HEAP_MIN_SLOTS=1000000

export RUBY_HEAP_SLOTS_INCREMENT=1000000

export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1 

export RUBY_GC_MALLOC_LIMIT=1000000000

export RUBY_HEAP_FREE_MIN=500000

下面我們來解釋一下這些變量的意義:

RUBY_HEAP_MIN_SLOTS

This specifies the initial number of heap slots. The default is 10000.

專門聲明堆棧的初始數據的,默認是10000.

這個默認配置太小了。因爲我們測試可能用到更多的堆,所以我們把它設置的大點。這就意味着我們要消耗更多的內存。

RUBY_HEAP_SLOTS_INCREMENT

The number of additional heap slots to allocate when Ruby needs to allocate new heap slots for the first time. The default is 10000.

For example, suppose that the default GC settings are in effect, and 10000 Ruby objects exist on the heap (= 10000 used heap slots). When the program creates another object, Ruby will allocate a new heap with 10000 heap slots in it. There are now 20000 heap slots in total, of which 10001 are used and 9999 are unused.

當Ruby需要開闢一片新的堆棧所需的數,默認是10000.

例如, 假如實際堆棧就只有默認配置的那麼多,有10000個Ruby對象已經佔滿了堆棧, 當你的程序創建另一個對象的時候, Ruby會分配一個新的堆,包含10000個棧。 那麼現在就一共有20000 個堆棧, 其中有10001個是被佔的, 而有9999空閒。

從這裏我們可以看出來, 當所有的桟都被耗盡,一片新的桟會被開闢出來。 默認配置的數字太低了,可能時不時的開闢堆,所以上面的配置把它增加了100倍。

RUBY_HEAP_SLOTS_GROWTH_FACTOR

Multiplicator used for calculating the number of new heaps slots to allocate next time Ruby needs new heap slots. The default is 1.8.

Take the program in the last example. Suppose that the program creates 10000 more objects. Upon creating the 10000th object, Ruby needs to allocate another heap. This heap will have 10000 * 1.8 = 18000 heap slots. There are now 20000 + 18000 = 38000 heap slots in total, of which 20001 are used and 17999 are unused.

The next time Ruby needs to allocate a new heap, that heap will have 18000 * 1.8 = 32400 heap slots.

 當ruby需要新的堆棧的時候, 此參數做爲一個乘數被用來計算這片新的堆棧的大小。

 拿上個例子來說, 假設程序又創建了10000個對象。Ruby需要分配另一個堆。這個堆將會是這麼多堆棧:

 10000 * 1.8 = 18000。 那麼這裏一共就有20000+18000個堆棧, 其中有20001個被用,而有17999個空閒。

 

RUBY_GC_MALLOC_LIMIT

The amount of C data structures which can be allocated without triggering a garbage collection. If this is set too low, then the garbage collector will be started even if there are empty heap slots available. The default value is 8000000.

這是不觸發垃圾回收的C數據結構的數量。如果它設置的太低就會觸發垃圾回收,即使還有很多可用的空閒堆棧。默認是8000000.

 

RUBY_HEAP_FREE_MIN

The number of heap slots that should be available after a garbage collector run. If fewer heap slots are available, then Ruby will allocate a new heap according to the RUBY_HEAP_SLOTS_INCREMENTand RUBY_HEAP_SLOTS_GROWTH_FACTOR parameters. The default value is 4096.

       在垃圾回收運行以後可用的堆棧數。 如果可用的堆棧太少, 那麼Ruby會按照UBY_HEAP_SLOTS_INCREMENT  和RUBY_HEAP_SLOTS_GROWTH_FACTOR的配置參數分配一個新的堆。默認是4096. 

值得注意的是,以上的配置請不要在生產環境下使用。 當然你可以參考37signals 和 Twitter 提供的REE配置來調優你的應用。

 

37signals 在生產環境的配置:

RUBY_HEAP_MIN_SLOTS=600000

RUBY_GC_MALLOC_LIMIT=59000000 

RUBY_HEAP_FREE_MIN=100000

Twitter 用在生產環境的配置:

RUBY_HEAP_MIN_SLOTS=500000 

RUBY_HEAP_SLOTS_INCREMENT=250000 

RUBY_HEAP_SLOTS_GROWTH_FACTOR=1 

RUBY_GC_MALLOC_LIMIT=50000000

 

關於Twitter配置的說明:

  • Start with enough memory to hold the application (Ruby’s default is very low, lower than what a Rails application typically needs).

  • 讓你的應用保持足夠的內存(Ruby默認的非常低, 低於典型Rails應用所需)

  • Increase it linearly if you need more (Ruby’s default is exponential increase).

  • 如果你需要更多堆棧,請保證它線性增長(Ruby默認的是指數級增長)

  • Only garbage-collect every 50 million malloc calls (Ruby’s default is 6x smaller).

  • 每5000萬個malloc被調用才啓動垃圾回收(Ruby默認的數比這個數小6倍)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章