Regex.Match Memory Leak

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples of what I have tried...

string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
while(true)
{
Regex .Match(testString,@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
while(true)
{
Regex rx = new Regex(testString);
Match m = rx.Match(@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
Regex rx;
Match m;
while(true)
{
rx = new Regex(testString);
m = rx.Match(@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
Regex rx;
Match m;
while(true)
{
rx = new Regex(testString);
m = rx.Match(@"(\w)");
rx = null;
m = null;
}

etc. etc. etc.
In all cases the memory grows to over 100megs in a matter of seconds and is never released. In my application the memory grows into the gigabytes and becomes unusable. Do you know what I can do to release the memory? If not do you know another way I can uses match functionality without getting this behaviour?

Cheers,

Jeff.
 
You can force a garbage collection with the statement:

GC.Collect()

--
Alex T. Silverstein
Systems Architect, Owner
The Unified Digital Group, LLC
Complete IT Consulting for Small Business
http://www.unifieddigital.com
Jeff McPhail said:
I am using Regex.Match in a large application and the memory is growing
out of control. I have tried several ways to try and release the memory and
none of them work. Here are some similar examples of what I have tried...
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
while(true)
{
Regex .Match(testString,@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
while(true)
{
Regex rx = new Regex(testString);
Match m = rx.Match(@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
Regex rx;
Match m;
while(true)
{
rx = new Regex(testString);
m = rx.Match(@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
Regex rx;
Match m;
while(true)
{
rx = new Regex(testString);
m = rx.Match(@"(\w)");
rx = null;
m = null;
}

etc. etc. etc.
In all cases the memory grows to over 100megs in a matter of seconds and
is never released. In my application the memory grows into the gigabytes and
becomes unusable. Do you know what I can do to release the memory? If not do
you know another way I can uses match functionality without getting this
behaviour?
 
Jeff said:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples of what I have tried...

string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
while(true)
{
Regex .Match(testString,@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
while(true)
{
Regex rx = new Regex(testString);
Match m = rx.Match(@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
Regex rx;
Match m;
while(true)
{
rx = new Regex(testString);
m = rx.Match(@"(\w)");

}
----------------------------------------------------------------------
string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
Regex rx;
Match m;
while(true)
{
rx = new Regex(testString);
m = rx.Match(@"(\w)");
rx = null;
m = null;
}

etc. etc. etc.
In all cases the memory grows to over 100megs in a matter of seconds and is never released. In my application the memory grows into the gigabytes and becomes unusable. Do you know what I can do to release the memory? If not do you know another way I can uses match functionality without getting this behaviour?

In my test (I only tested option 2 from above) running the loop in a
simple console application, I did not see the memory leak you're
describing. Memory usage for the app (as described by Task Manager)
ranged from 12MB to 20MB, generally staying in the lower end.

Do you see the problem only in your application or do you also see it if
you simply copy one of the above snippets into a Main() method that does
nothing else (I assume the latter, but it's not entirely clear from your
post).

I compiled and ran under Framework 1.1 on WinXP SP1.

One thing you might try to see if it makes a difference is something like:

string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf";
Regex rx = new Regex(testString);
while(true)
{
Match m = rx.Match(@"(\w)");
}

to see if creating the regex object only once makes any difference for
you (though it shouldn't).

Also, in your samples the string you pass to the the Match() method
looks more like a regex than the string you're using to create the regex
object. This probably doesn't matter for the test, but it will
definitely matter in the actual program.
 
Thanks, that completely solved the problem. I didn't realize that you had to (or even could) manually call garbage collection.
Jeff.
 
Back
Top