Memory management in ruby C extensions -
i've started working ruby c extensions. i'm having trouble understanding gc picks , not, , if can manually free 'native' ruby objects (like ruby arrays/strings) created in c snippet earlier on.
lets @ example illustrating question, first function allocates memory might, or might not free'd. second function manually free's memory , i'm wondering if break things in long run. see below background info on intend use it.
static value test_func(value self) { value ary, str1, str2; ary = rb_ary_new(); rb_ary_push(rb_str_new2("test-blabla")); rb_ary_push(rb_str_new2("test2-blabla")); return ary; }
now have figured out if run this, gc free on program termination, , therefore knows existance , i'm asuming gc sweep when running on code on daemon (i.e. background process).
my problem gc doesn't sweep enough wondering if adding function below hurt (e.g. break gc or something)
static value test_func_free(value self, value ary) { int i, len = rarray_len(ary); for(i = 0; < len; i++) { rb_str_free(rb_ary_entry(ary, i)); } rb_ary_free(ary); }
background info
i'm building server, answer clients need fiddle around in c bit. plan on returning answers in strings packed in array (a request can have multiple answers). when request answered wanted memory used answer gone, sooner better.
looking forward read answers,
~ michel
Comments
Post a Comment