TStringList of objects taking up tons of memory in Delphi XE

后端 未结 10 1447
遇见更好的自我
遇见更好的自我 2021-01-05 14:50

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 field

相关标签:
10条回答
  • 2021-01-05 15:35

    Starting with Delphi 2009, not only strings but also every TObject has doubled in size. (See Why Has the Size of TObject Doubled In Delphi 2009?). But this would not explain this increase if there are only 85,000 objects. Only if these objects contain many nested objects, their size could be a relevant part of the memory usage.

    0 讨论(0)
  • 2021-01-05 15:38

    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.

    0 讨论(0)
  • 2021-01-05 15:38

    I also read in a lot of strings in my program that can approach a couple of GB for large files.

    Short of waiting for 64-bit XE2, here is one idea that might help you:

    I found storing individual strings in a stringlist to be slow and wasteful in terms of memory. I ended up blocking the strings together. My input file has logical records, which may contain between 5 and 100 lines. So instead of storing each line in the stringlist, I store each record. Processing a record to find the line I need adds very little time to my processing, so this is possible for me.

    If you don't have logical records, you might just want to pick a blocking size, and store every (say) 10 or 100 strings together as one string (with a delimiter separating them).

    The other alternative, is to store them in a fast and efficient on-disk file. The one I'd recommend is the open source Synopse Big Table by Arnaud Bouchez.

    0 讨论(0)
  • 2021-01-05 15:39
    • In Delphi 2007 (and earlier), a string is an Ansi string, that is, every character occupies 1 byte of memory.

    • In Delphi 2009 (and later), a string is a Unicode string, that is, every character occupies 2 bytes of memory.

    AFAIK, there is no way to make a Delphi 2009+ TStringList object use Ansi strings. Are you really using any of the features of the TStringList? If not, you could use an array of strings instead.

    Then, naturally, you can choose between

    type
      TAnsiStringArray = array of AnsiString;
      // or
      TUnicodeStringArray = array of string; // In Delphi 2009+, 
                                             // string = UnicodeString
    
    0 讨论(0)
提交回复
热议问题