mrubyのメモリ使用量 on chipKit Max32
01/26追記: 測り方間違ってるぽいです! こちらを参照ください。mrubyのメモリ使用量(リベンジ) - kyabの日記 以下誤り!
chipKit Max32上でのmrubyのメモリ使用量を調べてみました。といってもmrb_open()しただけの状態です。
mrbconf.hはこんな感じで、TIME,GEMSをDisableにしてます。
/* -DDISABLE_XXXX to drop the feature */ #define DISABLE_REGEXP /* regular expression classes */ //#define DISABLE_SPRINTF /* Kernel.sprintf method */ //ok //#define DISABLE_MATH /* Math functions */ //ok #define DISABLE_TIME /* Time class */ //ng //#define DISABLE_STRUCT /* Struct class */ //ok //#define DISABLE_STDIO /* use of stdio */ //ok /* Now DISABLE_GEMS is added as a command line flag in Rakefile, */ /* we do not need to set it here. */ #define DISABLE_GEMS
検証コード。mrb_open()の直前と直後でmallocして、アドレスを出しています。スタックの底、ヒープの範囲はHow to Measure Available Memory?を参考にしました。
#define CHANGE_HEAP_SIZE(size) __asm__ volatile ("\t.globl _min_heap_size\n\t.equ _min_heap_size, " #size "\n") #include <mrbconf.h> #include <mruby.h> extern __attribute__((section("linker_defined"))) char _heap; extern __attribute__((section("linker_defined"))) char _min_heap_size; void setup(){ CHANGE_HEAP_SIZE(100*1024); Serial.begin(9600); int *p = (int *)malloc(sizeof(int)); Serial.print("before mrb_open(): allocated addres = "); Serial.println((unsigned int)p, HEX); mrb_state *mrb = mrb_open(); int *p2 = (int *)malloc(sizeof(int)); Serial.print("after mrb_open(): allocated addres = "); Serial.println((unsigned int)p2, HEX); byte *pTopHeap = (byte *)&_heap + (unsigned int) &_min_heap_size; Serial.print ('\n'); Serial.print("Bottom of stack: "); Serial.println((unsigned int) &pTopHeap, HEX); Serial.print("top of heap: "); Serial.println((unsigned int) pTopHeap, HEX); Serial.print("Bottom of heap: "); Serial.println((unsigned int) &_heap, HEX); } void loop(){ }
結果
before mrb_open(): allocated addres = A0000FD8 after mrb_open(): allocated addres = A00075F0 Bottom of stack: A001FFC8 top of heap: A0019FD0 Bottom of heap: A0000FD0
0xA00075F0 - 0xA0000FD8 = 26148byte消費していることになります。
試しにSPRINTF,STRUCT,STDIOをDisableにしてみたのですが、500byte程減っただけでした。
う〜ん、25kbですか。chipKit Max32のRAMは128kbなので、大分余裕があるように思えます。
でも、mruby-NTXのプロジェクトの挑戦 を見ると、最初98kbで、それをチューニングして64kbのRAMに収めたとある・・。測り方間違ってるのかなぁ。
あと、実際の動作コードでどれくらいの事ができるのか(どれくらい呼び出しネストできる?、どれくらいオブジェクト作れる?)も調べたいところ。