3

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
fishinear
  • 6,101
  • 3
  • 36
  • 84
  • Instead of NSRegularExpression, you can choose to use the POSIX `` API which has manual memory management and is more lightweight. –  Jul 14 '12 at 17:18
  • Thanks, @H2CO3. Unfortunately I cannot use POSIX, because I need Unicode support. But I found RegexKit: http://regexkit.sourceforge.net/#Latest, and am going to give it a try – fishinear Jul 14 '12 at 18:27
  • POSIX *does* support Unicode. –  Jul 14 '12 at 19:02
  • Does it? Then can you post a link to a page that shows how to use Unicode strings with POSIX? For example, which encoding is assumed? – fishinear Jul 14 '12 at 19:40
  • I was wrong, my bad -- POSIX's Unicode support isn't implicit. But you can still try PCRE. –  Jul 14 '12 at 20:00

1 Answers1

1

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.

Community
  • 1
  • 1
moxy
  • 1,634
  • 1
  • 11
  • 19
  • 1
    Thanks for the response, @moxy. Actually, I think the line you quote refers to the regular expressions built into NSPredicate, and is problably because they need to compile the regexp pattern on the fly. As NSRegularExpression compiles it in advance, it should be fast with O(n) while matching. I do not see anything about memory usage in the performance section, though. And alas, NSPredicate is a large overkill for my simple string matching. – fishinear Jul 14 '12 at 18:09