Home All Groups Group Topic Archive Search About

string.replace and outofmemoryexception

Author
21 Mar 2007 12:38 AM
pb
can someone suggest a better implementation than my sqlescaptestr function
below?

unfortunately this function throws an outofmemoryexception when i am
inserting multiple rows in a loop (str in such cases can be quite large...
could be upwards of 100K in some cases). 

i suppose the loop isn't allowing for the garbage collector to collect... i
could throw a GC in there, but I was hoping for a more memory efficient
function.


    Public Shared Function sqlescapestr(ByVal str As String) As String

        ' replaces ' with ''

        str = str.Replace("'", "''")
        Return str
    End Function

Author
21 Mar 2007 1:53 AM
VJ
always recommended to use StringBuilder (System.Text) class when performing
string operations

VJ

Show quote
"pb" <pb@newsgroups.nospam> wrote in message
news:B640390D-BB27-4440-8794-7C073CAFC6DA@microsoft.com...
> can someone suggest a better implementation than my sqlescaptestr function
> below?
>
> unfortunately this function throws an outofmemoryexception when i am
> inserting multiple rows in a loop (str in such cases can be quite large...
> could be upwards of 100K in some cases).
>
> i suppose the loop isn't allowing for the garbage collector to collect...
> i
> could throw a GC in there, but I was hoping for a more memory efficient
> function.
>
>
>    Public Shared Function sqlescapestr(ByVal str As String) As String
>
>        ' replaces ' with ''
>
>        str = str.Replace("'", "''")
>        Return str
>    End Function
>
Author
21 Mar 2007 7:26 AM
Jon Skeet [C# MVP]
VJ <nonewsaddr***@yahoo.com> wrote:
> always recommended to use StringBuilder (System.Text) class when performing
> string operations

For a simple string replacement like this, it wouldn't help at all.
Using StringBuilder when it isn't useful just makes code less readable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
21 Mar 2007 2:03 PM
VJ
Right.. but looks like there is a for loop (many times) in which this done,
so I reco'd moving to Stringbuilder.

Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.206aee6bd1595bef98d9d7@msnews.microsoft.com...
> VJ <nonewsaddr***@yahoo.com> wrote:
>> always recommended to use StringBuilder (System.Text) class when
>> performing
>> string operations
>
> For a simple string replacement like this, it wouldn't help at all.
> Using StringBuilder when it isn't useful just makes code less readable.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Author
21 Mar 2007 7:58 PM
Jon Skeet [C# MVP]
VJ <nonewsaddr***@yahoo.com> wrote:
> Right.. but looks like there is a for loop (many times) in which this done,
> so I reco'd moving to Stringbuilder.

It's being called multiple times, but on different strings - so again,
using a StringBuilder wouldn't help.

StringBuilder is useful when you're doing multiple operations on the
*same* string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
21 Mar 2007 9:24 PM
VJ
Ok Thanks John, did noto see that mulitple strings.. Yes I agree it works
with same string only

VJ

Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.206b9ecbdfe5949e98d9dc@msnews.microsoft.com...
> VJ <nonewsaddr***@yahoo.com> wrote:
>> Right.. but looks like there is a for loop (many times) in which this
>> done,
>> so I reco'd moving to Stringbuilder.
>
> It's being called multiple times, but on different strings - so again,
> using a StringBuilder wouldn't help.
>
> StringBuilder is useful when you're doing multiple operations on the
> *same* string.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Author
21 Mar 2007 7:26 AM
Jon Skeet [C# MVP]
pb <pb@newsgroups.nospam> wrote:
> can someone suggest a better implementation than my sqlescaptestr
> function below?

I'd suggest avoiding it to start with. Use parameterised queries
instead. That way the driver does whatever is necessary - far more
likely to be accurate.

> unfortunately this function throws an outofmemoryexception when i am
> inserting multiple rows in a loop (str in such cases can be quite large...
> could be upwards of 100K in some cases). 

That sounds very unlikely. I think it's more likely that you have a
different problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
21 Mar 2007 9:57 AM
Michael Nemtsev
Hello pb,

Try to profile your app to find out what's wrong.
As Jon noted the problem is obviously in other place

---
WBR,  Michael  Nemtsev [C# MVP]. 
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

p> can someone suggest a better implementation than my sqlescaptestr
p> function below?
p>
p> unfortunately this function throws an outofmemoryexception when i am
p> inserting multiple rows in a loop (str in such cases can be quite
p> large... could be upwards of 100K in some cases).
p>
p> i suppose the loop isn't allowing for the garbage collector to
p> collect... i could throw a GC in there, but I was hoping for a more
p> memory efficient function.
p>
p> Public Shared Function sqlescapestr(ByVal str As String) As
p> String
p>
p> ' replaces ' with ''
p>
p> str = str.Replace("'", "''")
p> Return str
p> End Function

AddThis Social Bookmark Button