Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSRegularExpression memory hog

I have been trying to bring down the memory usage of my app, and after profiling it turns out that NSRegularExpression is quite a memory hog.

One of my fistMatchInString calls (the first one?) allocates 1 MB of memory which is never released. And each call to firstMatchInString leaks a "NSConcreteValue" (whatever that is).

Have you experienced the same behavior, and do you know how to get around it? Do you know of good alternatives to NSRegularExpression?

Before you ask: all my regular expression patterns are small and created statically. All strings to match are small as well.

like image 290
fishinear Avatar asked Jun 15 '26 07:06

fishinear


1 Answers

Yes, regular expressions are expensive operations.

From the NSPredicate programming guide, it is said that : "Regular expression matching in particular is an expensive operation" and that "you should therefore perform simple tests before a regular expression".

I let you read the performance section of the predicate programming guide. I don't know if predicate can apply to your case but you might even extract some valuable info from the performance section (located in the "using predicate" chapter)

edit :

Look the regular expression part of this link, he is using a static NSRegularExpression instead of allocate it each time.

from this post, even if it's about python, the "+" seems to be more greedy, so there might be some improvement that can be made in your regex, in the expression itself, some best practices to implement.

like image 121
moxy Avatar answered Jun 17 '26 22:06

moxy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!