Memory Efficiencies

Former Member
Former Member
I have been looking at the cost of objects in memory to understand where all my memory is going.

Some simple experiments using the simulator to view memory has shown that
instancing a class with no instance vars and no functions (i.e. empty class) takes 96 bytes
instancing a class with no instance vars and one empty function takes 96 bytes
instancing a class with 1 instance vars and no functions takes 108 bytes
instancing a class with 2 instance vars and no functions takes 120 bytes
instancing a class with 1 instance var initialized to a long string 108 bytes

so there is a standard overhead of 96 bytes per instance and each instance var in the class takes 12 bytes. Storage for the actual object referenced in the instance var is held elsewhere.
Functions don't add to the size of an instanced class as expected.

The test class didn't extend any other class but extending another class adds an extra 60 bytes of overhead.

These sizes have surprised me greatly given the target device is very memory limited.

In the past when I have implemented object orientated design patterns on K&R C you just needed a couple of pointers for each instance - one to the class definition and one to the block of storage holding the instant vars. With a dynamic language things are a bit more complicated and I can see you needing each instance holding a pointer per instance var.

I can also see you may want to move some class level information into an instance to reduce pointer chasing, add a reference count and maybe a few flags, but the overhead per class instance and per instance var seems far to high for what is needed.

I am curious to know more about the runtime and VM to understand this but haven't seen any documentation - not too surprised there.

Dave.
  • There are probably a few things at play here. I really know nothing of the implementation, most of this is what I've gathered by experience. If you really want the answers, you can probably decompile the compiler code and have a look.

    So, you must remember that every class you declare implicitly extends Lang.Object. This adds overhead to every class automatically. Every class/module appears to be implemented as a Dictionary. The dictionary maps from a symbol name to the object in question, so when you declare a variable x in your class, an entry for that symbol is added to the class dictionary automatically by the compiler.

    Travis