Delphi 中的递归类实例大小

发布于 2024-07-12 23:03:45 字数 547 浏览 4 评论 0原文

Delphi中有没有办法获取类实例的实际大小?

我知道 TObject 类的 InstanceSize 方法,但该方法不会为对象成员递归调用自身。 例如,假设我们有两个类:

type MyClass1 = class
  private
    myVar1 : integer;
    myVar2 : integer;
end;

type MyClass2 = class
  private
    myOtherVar1 : integer;
    myOtherVar2 : MyClass1;
end;

对于这段代码,MyClass1 的长度为 12 字节(每个整数 4 字节加上 4 类开销),MyClass2 的长度为 24 字节(4 字节为类开销, myOtherVar2 中的 12 个字节和 myOtherVar1 整数中的另外 4 个字节)。 使用 InstanceSize 将导致每个实例占用 12 个字节,因为 myOtherVar2 被解释为指针(4 个字节)而不是类引用。

有没有办法获取类的总大小,包括它对其他类实例的引用?

Is there a way to get the actual size of a class instance in Delphi?

I know about the InstanceSize method of the TObject class but that method does not recursively invokes itself for object members. For example, let's say we have two classes:

type MyClass1 = class
  private
    myVar1 : integer;
    myVar2 : integer;
end;

type MyClass2 = class
  private
    myOtherVar1 : integer;
    myOtherVar2 : MyClass1;
end;

for this segment of code, MyClass1 will be 12 bytes length (4 bytes for each integer plus 4 for the class overhead) and MyClass2 will be 24 bytes lengh (4 bytes for the class overhead, 12 bytes from myOtherVar2 and another 4 for the myOtherVar1 integer). Using InstanceSize will result on 12 bytes for each of them since myOtherVar2 is interpreted as a pointer (4 bytes) instead of as a class reference.

Is there a way to get the total size of the class including its reference to other class instances?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

書生途 2024-07-19 23:03:45

构造一个 MyClass1 对象和一百万个 MyClass2,使得每个 MyClass2 都指向同一个 MyClass1。

每个 MyClass2 占用多少内存? 12.000012 字节?

循环列表占用多少内存? 无限,因为你可以永远追逐指针?

在带有指针的语言中,简单的递归大小算法通常没有用处。 您需要编写自己的算法,其中包含有关特定于您如何使用对象的聚合/组合、共享和递归引用的知识。

Construct one MyClass1 object and a million MyClass2 such that each MyClass2 points to the same MyClass1.

How much memory does each MyClass2 take? 12.000012 bytes?

How much memory does a circular list take? Infinity as you can keep chasing pointers for ever?

In languages with pointers, a naive recursive size-of algorithm isn't useful in general. You need to write your own algorithm which embodies knowledge about the aggregation/composition, sharing and recursive references specific to how you're using the objects.

小…红帽 2024-07-19 23:03:45

不,你想要的不存在。 如果你想要这样的东西,你应该自己写。

No what you want does not exist. If you want something like that, you should write it yourself.

焚却相思 2024-07-19 23:03:45

有没有办法获取总大小
类的名称,包括其引用
到其他类实例?

你刚才说了。 引用是一个指针; 它的大小是 4 字节。 InstanceSize 返回的值是为类的实例数据分配的字节数。

例如,myOtherVar2 可能为零。 但是nil指针值仍然会占用4个字节的内存。

Is there a way to get the total size
of the class including its reference
to other class instances?

You just said it. The reference is a pointer; its size is 4 bytes. The value returned by InstanceSize is the number of bytes allocated for instance data of the class.

myOtherVar2 might be nil, for example. But the nil pointer value would still occupy 4 bytes of memory.

‘画卷フ 2024-07-19 23:03:45

要了解它使用了多少内存,您可以让对象不被释放& 让 FastMM 告诉您泄漏的大小。

to find out how much memory it uses, you could let the objects not get freed & let FastMM tell you the size of the leak.

荒路情人 2024-07-19 23:03:45

听起来您想计算对象使用的内存。

如果您需要这样做,您可以检查 FastMM 是如何做到的,并且可能会在创建您类型的对象时挂钩您的过程。

工作量大,目标不明确; 在开始之前你最好有一个充分的理由。

It sounds like you want to count memory used by your objects.

If you need to do that, you can check how FastMM does it, and may be hook your procedure when objects of your type get created.

A lot of work with unclear goal; You better have a good reason before starting it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文