0

I need to minimize the number of memory allocations in a text-processing inner loop containing a number of calls to Regex.Matches(). So, the question, given a text length (in characters) t, a regex pattern length (in characters) of r and the number of matches m, what is the approximate number of memory allocations made by Regex.Matches(), and what is the size of these allocations? Are these influenced by any of the Regex options?

I've tried going through the source code of Regex and its associated classes, but am stymied by their complexity. Also did not find much documentation on the web related to the memory usage of Regex.Matches(). Hence the question.

bright
  • 4,700
  • 1
  • 34
  • 59
  • A regex is kind of like a FSM so it should not take up any more space than the memory (probably a hash table?) it takes to store it and probably some extra space for backtracking. So it would be influenced by the settings and the actual regex (the regex length has nothing to do with it, its complexity and certain feature usage does), number of matches should not matter (besides the actual results I guess). Tried any profiling yet? – MarZab Sep 25 '16 at 21:19
  • I already understand the concept; the implementation is what the question is about. – bright Sep 27 '16 at 05:47
  • Somewhat related: [C# - Concurrent Foreach Like Thread System](https://stackoverflow.com/questions/44870173/c-sharp-concurrent-foreach-like-thread-system) – Theodor Zoulias Jan 20 '21 at 18:32

1 Answers1

1

Get memory profiler like a JetBrains dotMemory, profile a simple application using regular expression and look at the memory traffic produced by Regex.Matches or other method you are insterested in. It's the simplest and the most accurate way to learn how many allocations does Regex.Matches(). Memory profiler embedded to Visual Studio does not show information about memory traffic, AFAIK, other commercial profilers should do it.

Ed Pavlov
  • 2,353
  • 2
  • 19
  • 25