I noticed a question over on the forums about how many objects are created when working with Introspection. The poster noticed that when they called GetType on a BevelButton instance, the Runtime.ObjectCount went over 3000 objects, even after the TypeInfo object goes out of scope! That seems like an extrodinarily high number, to be sure. And it may seem disconcerting that the object count doesn't drop.
However, this is not a bug. The Introspection module caches everything in sight, because otherwise there are performance implications when you try to use introspection. So let's run through BevelButton to understand these numbers and needs. Remember: every time we reference a new class type, we introspect it.
BevelButton is a RectControl, so that gets pulled in. RectControl has a property that denotes which Window the control is on, which pulls in Window. Window has a MenuBar property, which has MenuItem, which has Icon as Picture. Window also has DropObject, which takes a DragItem, which has a FolderItem, which has Movie, Picture, Sound, Date, and so on. You can begin to see why there are 3000 objects being pulled in -- there's a lot of stuff there! Now imagine if we didn't bother keeping any of that information around between subsequent TypeInfo objects -- it would be a performance nightmare. So, instead, TypeInfo shares all of the introspection data that it can. In fact, it shares everything except for the object instance itself (which allows you to get and set values on a specific class instance).
3000 objects may seem large -- but it's a one-time hit, and it means you have instant access to introspection data for a large piece of the framework. So it's not a bad thing, I promise. :-)
Leave a comment