Delphi:将数据存储在类vs记录中,减少内存使用 [英] Delphi: storing data in classes vs records, memory usage reduction

查看:154
本文介绍了Delphi:将数据存储在类vs记录中,减少内存使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序工作时,我有相当多的数据来存储,读取和修改内存。数据可以与树相比较,其中每个节点由有限数量的字符串和整数描述,并且具有相当多的子元素。
当前使用类/对象存储数据,如

I have quite a lot of data to store, read and modify in memory while the application works. The data could be compared to a tree, where each node is described by limited number of strings and integers, and has quite a lot of subelements. Currently the data is stored using classes/objects, like

TRootElement = class
  fName, fDescription: string;
  fPos: integer;
  /// etc
end;

fDocs: TObjectList; //list of TVariable = class(TRootElement)
fClasses: TObjectList; // list of TClass=class(TRootElement)



我正在寻找解决方案来限制它。

currently the memory consumed by the program is unacceptable, thus I'm looking for the solution to limit it.

我的问题是:如果我替换当前,OOP和基于对象的架构,一个基于记录?
例如,一般记录可以包含:

My question is: will the consumption be significantly reduced if I replace current, OOP and Objects based architecture with one based on records? For example, the general record could contain:

TRootElement = record
  fType: TElemType; // enum: root, variable, class, etc ... 
  fName, fDesc: string; 
  // all the fields used by root elem and it's descendants there
end;

我应该将TList替换为下一个/上一个元素的指针吗?因为我从来没有通过索引访问list的元素,我总是循环遍历整个列表,它不应该是真的很难做...但我想避免它,如果没有必要。

Should I replace TList with pointers to next / previous elements? Since I'm never accessing the elements of list by index, I'm always looping through the whole list, it shouldn't be really hard to do... however I'd like to avoid it if not necessary.

谢谢!
m。

Thanks! m.

推荐答案

将类更改为记录将减少内存使用,类或记录中的字段数增加。类和相应记录之间的大小差异正好是四个字节,这代表 VMT指针,一个类持有,但在记录中不存在。当考虑这种折衷时,这种差异通常是可以忽略的:为了节省4个字节,你放弃了继承,多态,数据隐藏和其他面向对象的特性。 (其中一些可能会被Delphi的新记录与方法减轻,但如果你只有Delphi 2005,你还没有这个功能。)

Changing a class into a record will reduce memory usage, but the significance of the savings decreases as the number of fields in the class or record increases. The size difference between a class and the corresponding record is exactly four bytes, which accounts for the VMT pointer that a class holds but which is absent from a record. That difference is usually negligible when you consider the trade-off: To save four bytes, you give up inheritance, polymorphism, data-hiding, and other object-oriented features. (Some of that might be mitigated with Delphi's new "records with methods," but if you only have Delphi 2005, you don't have that feature yet.)

事实上,如果这四个字节真的对你的程序有所不同,那么你可能有一个更大的问题要解决。这四个字节的节省只是通过添加另一个节点到你的树。有了一个足够大的数据集,无论你做出任何一个节点多么小,因为你将无法将它们全部保留在内存中。您需要调查某种缓存方案,因此只有一些节点保存在内存中,其余节点保存在其他位置,例如在文件或数据库中。

In fact, if those four bytes really make the difference for your program, then you probably have a bigger problem to solve. That four-byte savings is wiped away simply by adding another node to your tree. With a large enough dataset, it won't matter how small you make any one node since you won't be able to keep them all in memory anyway. You'd need to investigate some kind of caching scheme, so only some nodes are kept in memory and the rest are kept elsewhere, such as in a file or a database.

如果使用双向链接节点列表替换当前列表,则可能会看到内存使用增加,因为现在每个节点都在跟踪其下一个和上一个邻居,而在 TObjectList 正在管理它自己。

If you replace your current lists with doubly linked lists of nodes, you'll probably see your memory use increase because now each of your nodes is keeping track of its next and previous neighbors whereas before the TObjectList was managing all that itself.

这篇关于Delphi:将数据存储在类vs记录中,减少内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆