Question

I have a large delphi application and I am trying to keep the memory footprint low.

In my program I am using a component (Taco) and a component (TBurrito) that inherits from taco. Now just concerning the memory usage of the classes and not the actual instances, which scenario uses more memory?

A. Using just the TBurrito component everywhere

or

B. Using a combination of Taco and TBurrito components?

My one thought is that since TBurrito inherits Taco, the class Taco is already stored in memory and therefore using it will not increase the footprint of memory that much.

*Note - The component names are not really Taco and Burrito.

Was it helpful?

Solution

Every instance of Burrito will occupy at least as much memory as an instance of Taco. Subtract Taco.InstanceSize from Burrito.InstanceSize to find out how much more.

Using Burrito exclusively will not save you any memory; the definition of Taco will still exist, even if you have no instances of that exact class, because, at the very least, Burrito.ParentClass still needs to refer to it.

Use the smallest component that achieves your needs, but unless Burrito is huge compared to Taco or you have a large number of Burrito instances that could be Taco instances instead, you're probably not going to see much overall effect on your memory usage. That will come from refraining from loading entire forms, or loading just pieces of a file instead of the whole thing.

OTHER TIPS

Classes just use memory for their VMTs. Until you actually instance a class, it doesn't occupy space but for the VMT, and there is only one VMT for each class. The VMT size depends only on how many virtual methods a class actually has, because there is one entry for each virtual method. Static methods are resolved at compile time and don't use memory space. Other VMT data are fixed in size (although can be different in different version of Delphi). Dynamic methods were introduced to keep VMTs smaller. That's because inheriting a class will create a new VMT with all the "slots" of virtual methods of the parent class, plus the ones of the inherited class. Dynamic methods use run-time dispatching code to look for the method to be called. Because they are somewhat slower, their use was suggested only for classes that have overriden only a few methods of very large parent classes. If memory is not a problem, there are not reasons to use them. What could also use memory space are RTTI informations, although I never investigate where how they are stored. Anyway, if you use a child class, its parent VMT should be needed as well, because the child class may call inherited ones. But unless you use very large class with a lots of virtual methods and few instances, I guess most of the memory used by your application will be that of class instances, not class VMTs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top