Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tuning Ruby's GC

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Tuning Ruby's GC

A recipe for tuning MRI's GC, plus a shallow tour of how allocation and collection works.

Avatar for George Ogata

George Ogata

August 21, 2013

Other Decks in Programming

Transcript

  1. Twitter (Ruby 1.8.6) GC_MALLOC_LIMIT 50,000,000 HEAP_MIN_SLOTS 500,000 HEAP_SLOTS_ GROWTH_FACTOR 1

    HEAP_SLOTS_INCREMENT 250,000 (April 2009) 5 Wednesday, August 21, 13
  2. Ruby Objects Immediates: Symbol, Fixnum, true, false, nil Everything else:

    pointer to RObject flags klass ...data... ...data... ...data... 7 Wednesday, August 21, 13
  3. Ruby Objects Immediates: Symbol, Fixnum, true, false, nil Everything else:

    pointer to RObject flags klass len ptr capacity ...buffer... 7 Wednesday, August 21, 13
  4. Allocation def allocate if freelist empty run gc if freelist

    still empty allocate new heap object = freelist.pop # may malloc() set_up_object(object) object 9 Wednesday, August 21, 13
  5. Collection def gc for each root mark(root) sweep() def mark(object)

    if object not yet marked mark object object.mark() def sweep for each heap for each object in heap if object not marked add object to freelist object.free() 10 Wednesday, August 21, 13
  6. Adaptation next_heaps_length = heaps_used * initial_growth_factor malloc_limit += (inc -

    malloc_limit) * num_live_objects / num_objects free_min = heaps_used * HEAP_OBJ_LIMIT * 0.2 11 Wednesday, August 21, 13
  7. gdb `which ruby` PID (gdb) p ruby_current_vm->objspace->malloc_params $1 = {limit

    = 60609413, increase = 9542176} (gdb) p ruby_current_vm->objspace->heap $2 = {increment = 0, ptr = 0x14b325f0, sweep_slots = 0x0, sorted = 0x15686ac0, length = 8377, used = 8291, freelist = 0x9f45138, range = {0xa48c10, 0x1a997978}, freed = 0x0, live_num = 733492, free_num = 2642474, free_min = 678203, final_num = 0, do_heap_free = 2204162} 15 Wednesday, August 21, 13
  8. gdb `which ruby` PID (gdb) p ruby_current_vm->objspace->malloc_params $1 = {limit

    = 60609413, increase = 9542176} (gdb) p ruby_current_vm->objspace->heap $2 = {increment = 0, ptr = 0x14b325f0, sweep_slots = 0x0, sorted = 0x15686ac0, length = 8377, used = 8291, freelist = 0x9f45138, range = {0xa48c10, 0x1a997978}, freed = 0x0, live_num = 733492, free_num = 2642474, free_min = 678203, final_num = 0, do_heap_free = 2204162} HEAP_MIN_SLOTS = heap.length * 16384 / 40 15 Wednesday, August 21, 13