New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak with Regex #13712
Comments
Shrank up the example a little and decided to try using require "benchmark"
counts = Hash(Int64, Int64).new(0i64)
1_000.times do
emotes = [
":D",
]
text = "a long message with nothing in it a long message with nothing in it a long message with nothing in it a long message with nothing in it"
total = Benchmark.memory {
emotes.each do |emote|
regex = Regex.escape(emote)
regex += "\\b" if emote.matches?(/\w$/)
text.matches?(Regex.new(regex))
end
}
counts[total.to_i64] += 1
{% if flag?(:gc_collect) %}
GC.collect
{% end %}
end Here's an example of the outputs I got from this with different variances of building where the Key is the amount of memory used, and the value is the number of times that exact amount was used.
|
If you replace the |
As discussed in the Discord, it's possible this is just more of a case of #3997 In my case, we're running a TON of regex on web socket connections for parsing chat messages, and with the switch to PCRE2, this doubled our memory use. It seems our app does indeed have a memory leak, but it's most likely not regex. It was just that we had 4GB of ram, and switching to PCRE2 made our leak more prominent. For now, we can just close this out. I'll open a new issue if I have a more concrete example on what's going on. |
Using PCRE 1, it seems this script will increase in memory, but slowly and top out at
3933458
fortotal_bytes
, but using PCRE 2 will increase in memory quick and top out at6034353
.crystal build -Duse_pcre regex.cr
vscrystal build -Duse_pcre2 regex.cr
Can someone confirm if I'm reading this correctly?
The text was updated successfully, but these errors were encountered: