在Delphi XE中占用大量内存的对象的TStringList [英] TStringList of objects taking up tons of memory in Delphi XE

查看:517
本文介绍了在Delphi XE中占用大量内存的对象的TStringList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个模拟程序。



程序的第一件事情是在一个巨大的文件(28 mb,约79000行) ,),解析每行(约150个字段),为对象创建一个类,并将其添加到TStringList。



它还读入另一个文件,其中添加更多的对象在运行。最后,它最终是约85'000个对象。



我正在使用Delphi 2007,该程序使用了很多内存,但运行正常。我升级到Delphi XE,并将程序迁移到现在,它使用了更多的内存,最终在运行中一半的内存不足。



所以在Delphi 2007中,最初在初始文件中使用1.4 gig,这显然是一个巨大的数量,但是在XE中,最终使用了近1.8个gig,这真的是巨大的,导致用完了



所以我的问题是


  1. 为什么使用这么多内存?

  2. 为什么XE比2007年使用更多的内存?

  3. 我可以做些什么?我不能改变文件的大小或长度,我需要为每一行创建一个对象,并将其存储在某个地方。

谢谢

解决方案

很难说你的28 MB文件扩展到1.4 GB的对象,当你解析出来进入对象而不看代码和类声明。另外,你说你将它存储在一个 TStringList 而不是一个 TList TObjecList 。这听起来像您使用它作为某种字符串 - >对象键/值映射。如果是这样,您可能需要查看XE中 Generics.Collections 单元中的 TDictionary 类。 string 的类型从ANSI字符串更改为UTF -16字符串在Delphi 2009中。如果你不需要Unicode,你可以使用一个TDictionary来节省空间。



另外,为了节省更多的内存,还有一个技巧你可以使用,如果你不需要所有79,000的对象马上:懒加载。这个想法是这样的:




  • 将文件读入TStringList。 (这将使用大约与文件大小一样多的内存,如果它被转换为Unicode字符串可能是两倍)。不要创建任何数据对象。

  • 当你需要一个特定的数据对象,调用检查字符串列表并查找该对象的字符串键的例程。

  • 检查该字符串是否具有与之关联的对象。如果没有,请从字符串中创建对象,并将其与TStringList中的字符串相关联。

  • 返回与字符串相关联的对象。



这将保持内存使用和加载时间的减少,但只有在加载后不需要所有对象(或大部分)的对象才有用。 p>

I'm working on a simulation program.

One of the first things the program does is read in a huge file (28 mb, about 79'000 lines,), parse each line (about 150 fields), create a class for the object, and add it to a TStringList.

It also reads in another file, which adds more objects during the run. At the end, it ends up being about 85'000 objects.

I was working with Delphi 2007, and the program used a lot of memory, but it ran OK. I upgraded to Delphi XE, and migrated the program over and now it's using a LOT more memory, and it ends up running out of memory half way through the run.

So in Delphi 2007, it would end up using 1.4 gigs after reading in the initial file, which is obviously a huge amount, but in XE, it ends up using almost 1.8 gigs, which is really huge and leads to running out and getting the error

So my question is

  1. Why is it using so much memory?
  2. Why is it using so much more memory in XE than 2007?
  3. What can I do about this? I can't change how big or long the file is, and I do need to create an object for each line and to store it somewhere

Thanks

解决方案

It's hard to say why your 28 MB file is expanding to 1.4 GB worth of objects when you parse it out into objects without seeing the code and the class declarations. Also, you say you're storing it in a TStringList instead of a TList or TObjecList. This sounds like you're using it as some sort of string->object key/value mapping. If so, you might want to look at the TDictionary class in the Generics.Collections unit in XE.

As for why you're using more memory in XE, it's because the string type changed from an ANSI string to a UTF-16 string in Delphi 2009. If you don't need Unicode, you could use a TDictionary to save space.

Also, to save even more memory, there's another trick you could use if you don't need all 79,000 of the objects right away: lazy loading. The idea goes something like this:

  • Read the file into a TStringList. (This will use about as much memory as the file size. Maybe twice as much if it gets converted into Unicode strings.) Don't create any data objects.
  • When you need a specific data object, call a routine that checks the string list and looks up the string key for that object.
  • Check if that string has an object associated with it. If not, create the object from the string and associate it with the string in the TStringList.
  • Return the object associated with the string.

This will keep both your memory usage and your load time down, but it's only helpful if you don't need all (or a large percentage) of the objects immediately after loading.

这篇关于在Delphi XE中占用大量内存的对象的TStringList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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