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

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

问题描述

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

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吗?因为我从来没有通过索引访问列表的元素,我总是循环遍历整个列表,这不应该是很难做的,但是如果不需要,我想避免它。

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指针一个类持有,但没有在记录中。当您考虑权衡时,这种差异通常可以忽略:要保存四个字节,您放弃继承,多态,数据隐藏和其他面向对象的功能。 (其中一些可能会用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:将数据存储在类与记录中,减少内存使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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