Freeing memory after string concatenation

Sean Kelly sean at invisibleduck.org
Tue Dec 23 17:58:14 PST 2008


Alex wrote:
> Hey,
> I got a problem after concatenating two strings:
> 
> void main()
> {
>        while(true) // For faking high program activity...
>        {
>              string t="Do";
>              string t2="That"
>              
>              foo(t,t2);
> 
>              delete t;
>              delete t2;

Not necessary in this case, since t and t2 will reference static memory.

>        }
> }
> 
> void foo(string s1, string s2)
> {
>        string con=s1~s2;
>        writeln(con);
>        delete con;

Here, a single allocation will take place for con.

> }
> 
> until now, there seems everything to be ok but now I want to make my program run most efficiently...
> I looked at the memory usage with the TaskManager
> I found that 'con' did not get removed! (the memory usage grew up very fast after several repeations)

You can't watch TaskManager to get an accurate measure of in-program 
memory use.  The GC allocates and potentially frees memory from the OS 
in blocks, and it does not do so very often.  Better to use the GC 
statistics feature if you want a detailed idea of what's going on.


Sean


More information about the Digitalmars-d-learn mailing list