Search code examples
jrubyjrubyonrails

Limiting the size of the Regular Expression Cache in JRuby


We are finding that the Regular Expression Cache in our JRuby application is out of control - it just keeps growing and growing until the app is grinding to a halt.

It eventually does garbage collect, but transaction time is becomes far too high (90 secs instead of 1-2 secs) long before that.

Is there a way to either stop this Regexp Cache from growing so much or limit the size of the cache?


Solution

  • first of all, since you already mentioned looking at the source at Very large retained heap size for org.jruby.RubyRegexp$RegexpCache in JRuby Rails App you probably realised there's no such support implemented.

    would say you have 2-3 options to decide :

    • implement support for limiting or completely disabling the cache within JRuby's RubyRegexp
    • introduce a "hack" that will check available memory and clear out some of the cache RubyRegexp caches e.g. from another thread (at least until a PR is accepted into JRuby)
    • look into tuning or using a different GC (including some JVM options) so that the app performs more predictably ... this is application dependent and can no be answered (in general) without knowing the specifics

    one hint related to how the JVM keeps soft references -XX:SoftRefLRUPolicyMSPerMB=250 it's 1000 (1 seconds) by default thus decreasing it means they will live shorter ... but it might just all relate to when they're collected (depends on GC and Java version I guess) so in the end you might find out to be fixing the symptom and not the real cause (as noted things such these can not be generalized esp. knowing very little about the app and/or JVM OPTS used)