Mega Code Archive

 
Categories / Delphi / Strings
 

Save memory with duplicate strings

Title: save memory with duplicate strings? { Often there are lots of duplicate strings in a program, for example take the following database: Author Title aaa xxx aaa yyy aaa zzz Suppose you want to read it into your program and create one record or class per line: } while not Table.EOF do begin author := Table['Author']; title := Table['Title']; MyClass := TMyClass(author, title); StoreItSomewhere(MyClass); Table.Next; end; { You end up with three different strings containting the same author. No problem with that as long as there are only three of them. Now suppose you have got a database of 100,000 entries and lots of duplicate author names. If you do the above again you will end up with a huge memory overhead due to fragmentation of the free memory on the heap. In addition to that freeing the objects will result in a noticable delay of the program while the memory manager is busy merging free memory blocks. On the other hand, Delphi's AnsiStrings use reference counting so it would be possible to assign the same string to many string variables without copying the contents: } Author := 'aaa'; for i:=0 to 100000 do begin MyClass := TMyClass(Author); StoreItSomewhere(MyClass); end; { This will create 100000 strings containting 'aaa', but store the actual contents only once, because they are assigned the same string constant. It would be nice to do the same with the above database example, wouldn't it? But how do we get Delphi to realize that the content is really the same? Answer: A StringList } authors := TStringList.Create; authors.Sorted := true; authors.Duplicates := dupIgnore; while not Table.EOF do begin author := Table['Author']; title := Table['Title']; authors.Add(author); authors.Search(author, Idx); author := authors[Idx]; MyClass := TMyClass(author, title); StoreItSomewhere(MyClass); Table.Next; end; authors.free; { This will only keep one string of each author's name and assign it to all other occurences, even after the StringList has been freed again. The wonders of reference counting! I admit that the above example looks a bit odd. Nobody would load such a database into memory. But I had a similar problem today and by using the trick above I managed to reduce the memory usage of my program from 120 megabytes to 10 (basically nothing, given that it started out with 8 mb). }