System.OutOfMemoryException was thrown. at Go60505(RegexRunner ) at System.Text.RegularExpressions.CompiledRegexRunner.Go()
I am a c# dev working on some code for a website in vb.net. We use a lot of caching on a 32bit iss 6 win 2003 box and in some cases run into OutOfMemoryException exceptions. This is the code I trace it back to and would like to know if anyone else has has this...
Public Sub CreateQueryStringNodes()
'Check for nonstandard characters'
Dim key As String
Dim keyReplaceSpaces As String
Dim r As New Regex("^[-a-zA-Z0-9_]+$", RegexOptions.Compiled)
For Each key In HttpContext.Current.Request.Form
If Not IsNothing(key) Then
keyReplaceSpaces = key.Replace(" ", "_")
If r.IsMatch(keyReplaceSpaces) Then
CreateNode(keyReplaceSpaces, HttpContext.Current.Request(key))
End If
End If
Next
For Each key In HttpContext.Current.Request.QueryString
If Not IsNothing(key) Then
keyReplaceSpaces = key.Replace(" ", "_")
If r.IsMatch(keyReplaceSpaces) Then
CreateNode(keyReplaceSpaces, HttpContext.Current.Request(key).Replace("--", "-"))
End If
End If
Next
End Sub
.NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
error:
Exception of type 'System.OutOfMemoryException' was thrown. at Go60505(RegexRunner ) at System.Text.RegularExpressions.CompiledRegexRunner.Go() at System.Text.RegularExpressions.RegexRunner.Scan(Regex regex, String text, Int32 textbeg, Int32 textend, Int32 textstart, Int32 prevlen, Boolean quick) at System.Text.RegularExpressions.Regex.Run(Boolean quick, Int32 prevlen, String input, Int32 beginning, Int32 length, Int32 startat) at System.Text.RegularExpressions.Regex.IsMatch(String input) at Xcite.Core.XML.Write.CreateQueryStringNodes() at Xcite.Core.XML.Write..ctor(String IncludeSessionAndPostedData) at 开发者_如何转开发mysite._Default.Page_Load(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at
thanks
There's a couple of articles out there that pretty much say avoid using Compiled
, it doesn't really mean what people think it means sometimes. If I understand correctly, using Compiled actually permanently eats up memory for the duration of the application. Since you're on the web, the application's lifetime might be fairly long. This was supposed to be fixed/addressed in the 2.0 Framework but it looks like it hasn't.
thanks! let me me check a few things in the office tomorrow, I might try a combination of both solutions - I will write some tests and let you know. the regex parser is acting on a long string, the create node is populating a list object that is later used like xml for xslt transformation for html output, we are using response output caching on the final output. I dont think the static variable will make a difference as this is a asp.net app the CreateQueryStringNodes is only called once through the page life cycle for each page. I might put it into app cache and use it as a singleton but the work to fetch it might be less efficient - let me test it...
thanks again.
So once the code replaces all the spaces with '_', it then matches them? Is this some kind of validation for alpanumeric with a dash or underscore?
How much data is being processed by the regex parser?
Note your pattern (as it is now) can be changed to
^[\w-_]+$
(Note your use of Regex Compiled is perfect for this situation, unlike what the other post says).
To find out about compilation check out To truly understand what is going on see: Base Class Library Performance Tips and Tricks at the section Regular Expression Compilation. HTH
精彩评论