<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On 8 Feb 2017, at 16:51, Tony Parker via swift-corelibs-dev <<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>> wrote:<br class=""><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 18px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Hi Nethra,</span><div class="" style="font-family: Helvetica; font-size: 18px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""><div class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 7, 2017, at 11:44 PM, Nethra Ravindran via swift-corelibs-dev <<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class="">Hi everyone,</div><div class=""><br class=""></div><div class="">I am working on <a href="https://bugs.swift.org/browse/SR-3536" class="">https://bugs.swift.org/browse/SR-3536</a></div><div class=""><p class="">There is a memory leak when searching for the substring of a string using regular expression.</p></div><div class=""><br class=""></div><div class=""><pre class="" style="box-sizing: border-box; font-family: consolas, 'liberation mono', menlo, courier, monospace; font-size: 11.899999618530273px; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: rgb(51, 51, 51);"><code class="" style="box-sizing: border-box; font-family: consolas, 'liberation mono', menlo, courier, monospace; padding: 0px; margin: 0px; background-color: transparent; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; border: 0px; display: inline; overflow: visible; line-height: inherit; word-wrap: normal;">import Foundation
let myString = "Foo"
for _ in 1...10000 {
let _ = myString.range(of: "bar", options: .regularExpression)
}</code></pre><p class="" style="font-family: helveticaneue; font-size: 12px;"><br class=""></p><p class="" style="font-family: helveticaneue;">From the above test case i could see that over a period of time, around 60 Mb of memory was leaked. </p><p class="" style="font-family: helveticaneue;">I see in String.range we eventually call NSString._createRegexForPattern. Here we maintain a mapping between NSString and NSRegularExpression object in NSCache<NSString, NSRegularExpression>. All the entries in the cache are maintained in a dictionary ( _entries ) which takes the UnsafeRawPointer as the key, which seems to be the address of the NSString Object and NSCachedEntry as value.</p><p class="" style="font-family: helveticaneue;">Though the pattern is of type String, it is stored in the NSCache as NSString. And since we are storing the NSCachedEntry objects in a dictionary indexed by the address (UnsafeRawPointer) of the NSString object, there is a new cache entry created for each iteration ( in the test case ) though the pattern string remains the same.</p><p class="" style="font-family: helveticaneue;">Can someone guide me about how to go about resolving this issue.</p></div></div></div></div></blockquote><br class="">Looks like you’ve done most of the analysis, so you’re already pretty much there. =)</div><div class=""><br class=""></div><div class="">Is there some other way we could be caching the results here?</div></div></div></div></blockquote><div><br class=""></div><div>There's a 'cache.countLimit = 10' set on the cache:</div><div><br class=""></div><div><a href="https://github.com/apple/swift-corelibs-foundation/blob/16657160c2c441a58ea01bf7baa90607a0b395f7/Foundation/NSString.swift#L109" class="">https://github.com/apple/swift-corelibs-foundation/blob/16657160c2c441a58ea01bf7baa90607a0b395f7/Foundation/NSString.swift#L109</a></div><div><br class=""></div><div>Shouldn't it start discarding some of the previous entries after it hits the first 10?</div><div><br class=""></div><div>Alex</div></div></body></html>