Ruby’s garbage collector tries to adapt memory usage to the amount of memory used by the program by dynamically growing or shrinking the allocated heap as it sees fit. For long running server applications, this approach isn’t always the most efficient one. The performance very much depends on the ratio heap_size / program_size. It behaves somewhat erratic: adding code can actually make your program run faster.
With REE, one can tune the garbage collector’s behavior for better server performance. It is possible to specify the initial heap size to start with. The heap size will never drop below the initial size. By carefully selecting the initial heap size one can decrease startup time and increase throughput of server applications.
Garbage collector behavior is controlled through the following environment variables. These environment variables must be set prior to invoking the Ruby interpreter.
- RUBY_HEAP_MIN_SLOTSThis specifies the initial number of heap slots. The default is 10000.
- RUBY_HEAP_SLOTS_INCREMENTThe 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_HEAP_SLOTS_GROWTH_FACTORMultiplicator 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_GC_MALLOC_LIMITThe 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.
- RUBY_HEAP_FREE_MINThe 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_INCREMENT and RUBY_HEAP_SLOTS_GROWTH_FACTOR parameters. The default value is 4096.
GC configuration With respect to my portal
As mentioned into coffeepowdered.net(reference link), we have used scrap plugin.It's given following statistic
As, you see in the statistic, we required 9 heaps slot to feet 2181349 objects. To reduce this to feet into single heap we have added following configuration.
1. Created a file with any name. e.g
2. Added following line in it
3. Given execute permission to it
4. As we using passenger, so we have modified nginx.conf to use above configuration,
with
After doing this configuration, we got following statistic,
As you can see two important variables are come down drastically.
GC cycles so far : 14
Number of heaps : 1
Reference
http://www.coffeepowered.net/2009/06/13/fine-tuning-your-garbage-collectorhttp://www.mikeperham.com/2009/05/25/memory-hungry-ruby-daemons/
No comments:
Post a Comment