Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8716259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:02:23+00:00 2026-06-13T06:02:23+00:00

Regex.CacheSize Property Gets or sets the maximum number of entries in the current static

  • 0

Regex.CacheSize Property
Gets or sets the maximum number of entries in the current static cache of compiled regular expressions.

The Regex class maintains an internal cache of compiled regular expressions used in >static method calls. If the value specified in a set operation is less than the current >cache size, cache entries are discarded until the cache size is equal to the specified >value.

By default, the cache holds 15 compiled static regular expressions. Your application >typically will not have to modify the size of the cache. Use the CacheSize property only >when you want to turn off caching or when you have an unusually large cache.

So I’d like to have insight into the current number of expressions in the cache. Anyone know if/how that is possible?

Idea being that I reuse < 15 of them now so don’t want to fiddle with the CacheSize, but would like to be able to check the actual cache usage at some point to either log if I am hitting the max (as regex usage expands) or dynamically adjust CacheSize.

Alternatively, any comments as to the overhead of simply increasing the CacheSize to some arbitrarily large number?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:02:23+00:00Added an answer on June 13, 2026 at 6:02 am

    Decompilation (of mscorlib 4.0) reveals that the cache is an internal linked list of CachedCodeEntry, so you’re not going to get at it without reflection.

    The overheads of increasing the maximum cache size would be:

    1. the memory cost of storing the cached entries; the usage of the maximum is simply in logic like this on Regex creation:

      • are we caching, in general?
        • if so, cache this regex
        • have we now exceeded the maximum cache size?
          • if so, remove the last cache entry

    2. the increased cost to traverse the cache looking for a match

    So long as your numbers aren’t absurd, you should be OK cranking it up.

    Here’s the reflection code you’d need to retrieve the current cache size:

        public static int RegexCacheSize()
        {
            var fi = typeof(Regex).GetField("livecode", BindingFlags.Static 
                                                      | BindingFlags.NonPublic);
            var coll = (ICollection)(fi.GetValue(null));
    
            return coll.Count;
        }
    

    We use the cast to ICollection to avoid the complication of having to cast to a generic list on an internal type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What RegEx would replace a single 'm' letter with (e.g.) a number, assuming that
RegEx to match if the string contains 1 or more sets of 1-10 alphanumerics
Using Regex Hero here are the inputs: Regular Expression: /category/* Replacement String: /experiment/$0 Target
Regex: \b< low= number= low=>\b Example string: <b22>Aquí se muestran algunos síntomas < low=
Current Regex: ^([\d-]*[A-z]?|[A-z]?[\d-]*|[\d-]*[A-z]?[\d-]*){3}$ (Allows a max of 3 letters and any amount of numbers
Regex expressions seems cocumbersome, they always drive me mad when trying to build it
[dbo].Regex above is my regular expression when i am passing '(' or ')'brackets symbol
Regex(@@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$, RegexOptions.Compiled); Using the above, I want to pass only values like @gmail.com, @xyz.edu,
The regex below replace the white space before the number with a comma: var
Next regex says any optional group of 1 to 3 digits plus any optional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.