I can't find much documentation on when it's appropriate to declare a VALUE
as volatile
in Ruby extensions to avoid premature garbage collection of in-use objects.
Here's what I've learned so far. Can anyone fill in the blanks?
When volatile
does not need to be used:
- in C++ object members (because they're never on the stack?)
- in C/C++ methods that do not call Ruby API (because the GC isn't turned on until Ruby API is called)
When volatile
does need to be used
- in C/C++ methods that call any of the Ruby
ALLOC
ormalloc
macros/functions (because these can trigger GC when memory is highly fragmented) - in C/C++ methods that call any Ruby functions (e.g.,
rb_funcall
,rb_ary_new
, etc.)
Other methods for avoiding GC
- mark
VALUE
s that are in use - disable the GC during object creation and then re-enable it
- don't use Ruby's allocation methods (risky)
Is everything correct? What details am I missing? Other than marking, is volatile
the only way?