Very large string building in Delphi
Posted on 2008-11-14
In one of my Delphi application (Turbo Delphi 2006), I have a loop that builds a very large string
It looks like this :
for I := 1 to N do
LargeString := LargeString + Something NotToSmall;
I need that LargeString for some other processing later on (too long to explain here)
At the end of this loop, LargeString may be several megabytes large.
This as such is not a problem, but I am afraid that in some occasion, the memory manager could have a kind of problem due to the repeated instruction "LargeString := LargeString + Something NotToSmall;"
LargeString becomes larger and larger.
I suppose that to execute this instruction, the memory manager will first be asked to allocate a bloc of length = total length of resulting string, and after, will be able to free the previous one.
But the freed place will not be reused because too small the next time, etc...
So at the end, a kind of short of memory could occur.
How can I avoid this to happen ? Alternative code ? Which one ?