Stringbuilder CLEARSTRING action

Stringbuilder CLEARSTRING action

Hi Guy's,

Is there a 'nice' way to clear the contents of / reinitialize a Stringbuilder object?

With nice, i mean not by:
- Creating a brand new object
- Assigning the contents of an empty object

but just clearing it out.


Errr... StringBuilders in OutSystems? Where? Where? Where??? :)

But anyway.

You could try something like sb.Remove(0, sb.Length);
The Text extension has a stringbuilder in it.
I'm afraid i'm no .NET coder so integrating the sb.Remove command would be kind of an issue for me.
I understand the not-nice way is not the preferred way, but I doubt if it would affect the performance.

Also, the "removed" chars will be "thrown" on the garbage-collector, wether you assign a new object to it or clear the string itself.

Hi Joost,

Thanks for your reply.
I'd like to use the 'nice' method because I'm not sure whether :

The Old object would "point" to the new object or would get it's contents (empty) with an assign.

The size of my Stringbuilder object is currently set to 120 Megabytes.
Per default I'm not really committed to checking memory usage, but in this case i'd like to try and save some. :)

Besides that, I think clearing the content is a pretty common request when addressing a reserved data block,
so I'm hoping the Outsystems R&D team picks this up as a request.
imho, if you do one or the other, the memory won't decrease or increase with 120Mb.
about the old data, well, the data is not zeroed if that what you want, but just marked as to be collected.

about the request  for R&D.
yes, it would improve readabillity.
no, because I dount if it would make any difference performance/memory wise.

To complement what has been said, even if someone were to add the Remove method (that the StringBuilder class in the .NET Framework alread has), it wouldn't realease the unused memory.

The .NET Framework clearly states that the Remove method *doesn't* change the capacity of the StringBuilder object, so if it has a capacity of 120MB before the Remove, it will still have that capacity after.

On the other hand, one can then reuse the object and reuse the already allocated memory, so it's not completely useless.

Hi Joost,

Thanks for your reply.

I doubt if the contents are actually zeroed when one assigns a different object to it.
(e.g. results an object assign in outsystems a change of the memory pointer or in a memory assign / copy)
The change of a memory pointer would give nasty situations and won't have the desired effect.

Looking at Carlos' reply; indeed a memory block gets reserved when defining a second object (to empty the other one)
Oh! The Remove method doesn't create a new object, it simply clears the specified part of the string of the StringBuilder. Probably not the best thing to do with really big strings...

And, if you absolutely must release all that memory used by an object, you could create a simple extension with a single action with the following code:


Although this may not actually work, given how objects are referenced until the <end> widget thingy. And even if it works, it'll freeze *all* ASP.NET apps running at that time while the garbage collector is running and that's probably not nice.