我的delphi应用程序的内存占用
我有一个大型delphi应用程序,我试图保持较低的内存占用。
在我的程序中,我使用一个组件(Taco)和一个继承自 taco 的组件(TBurrito)。现在只考虑类的内存使用情况而不是实际实例,哪种情况使用更多内存?
A. 在任何地方都只使用 TBurrito 组件
还是
B. 使用 Taco 和 TBurrito 组件的组合?
我的一个想法是,由于 TBurrito 继承了 Taco,所以 Taco 类已经存储在内存中,因此使用它不会增加太多内存占用。
*注意 - 组件名称实际上并不是 Taco 和 Burrito。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个
Burrito
实例将至少占用与Taco
实例一样多的内存。从Burrito.InstanceSize
中减去Taco.InstanceSize
以了解还多了多少。仅使用
Burrito
不会节省任何内存;即使您没有该确切类的实例,Taco
的定义仍然存在,因为至少,Burrito.ParentClass
仍然需要引用它。使用满足您需求的最小组件,但除非
Burrito
与Taco
相比巨大,或者您有大量Burrito< /code> 实例可能是
Taco
实例,您可能不会看到对内存使用的总体影响。这将来自于避免加载整个表单,或者仅加载文件的一部分而不是整个文件。Every instance of
Burrito
will occupy at least as much memory as an instance ofTaco
. SubtractTaco.InstanceSize
fromBurrito.InstanceSize
to find out how much more.Using
Burrito
exclusively will not save you any memory; the definition ofTaco
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 toTaco
or you have a large number ofBurrito
instances that could beTaco
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.类仅将内存用于其 VMT。在你实际实例化一个类之前,除了VMT之外,它不占用空间,并且每个类只有一个VMT。 VMT 大小仅取决于类实际有多少个虚拟方法,因为每个虚拟方法都有一个条目。静态方法在编译时解析,不使用内存空间。
其他VMT数据的大小是固定的(尽管在不同版本的Delphi中可能不同)。引入动态方法来保持 VMT 更小。这是因为继承一个类将创建一个新的 VMT,其中包含父类的虚拟方法的所有“槽”以及继承类的虚拟方法。动态方法使用运行时调度代码来查找要调用的方法。由于它们速度较慢,因此仅建议对仅覆盖非常大的父类的少数方法的类使用它们。如果内存不是问题,就没有理由使用它们。
RTTI 信息也可以使用内存空间,尽管我从未研究过它们是如何存储在哪里的。
无论如何,如果您使用子类,则也应该需要其父类 VMT,因为子类可能会调用继承的类。但除非您使用具有大量虚拟方法和少量实例的非常大的类,否则我猜您的应用程序使用的大部分内存将是类实例的内存,而不是类 VMT。
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.