From: Jeff Mitchell Date: 2004-06-10T08:42:48+09:00 Subject: spikes in memory usage The following is an example of some memory problems I've been having with my extension classes. This is on AthlonXP 1.5GHz Linux with gcc 3.3.2. require './myclass' size = ARGV.shift.to_i loop { MyClass.new(size) 1000.times { MyClass.new(size) } } size mem usage ------------------- 1 1.6 meg 10 1.6 meg 100 2.1 meg 1000 7.8 meg 10000 61.0 meg 100000 277.0 meg 1000000 15.0 meg 10000000 2.7 meg 100000000 1.6 meg extconf.rb ----------- require 'mkmf' create_makefile('myclass') myclass.c ---------- #include "ruby.h" struct MyStruct { char* stuff ; } ; void myclass_free(struct MyStruct* data) { free(data->stuff) ; free(data) ; } VALUE rb_myclass_initialize( VALUE self, VALUE size ) { struct MyStruct* data; Data_Get_Struct(self, struct MyStruct, data) ; data->stuff = (char*)malloc(NUM2LONG(size)*sizeof(char)) ; return self ; } static VALUE rb_myclass_s_allocate(VALUE klass) { struct MyStruct* data ; VALUE obj = Data_Make_Struct(klass, struct MyStruct, 0, myclass_free, data) ; return obj ; } VALUE cMyClass ; void Init_myclass() { cMyClass = rb_define_class("MyClass", rb_cObject) ; rb_define_alloc_func(cMyClass, rb_myclass_s_allocate) ; rb_define_method(cMyClass, "initialize", rb_myclass_initialize, 1) ; } __________________________________ Do you Yahoo!? Friends. Fun. Try the all-new Yahoo! Messenger. http://messenger.yahoo.com/