擅长:python、mysql、java
<p>当我在计算Levenshtein距离的函数中使用<code>pool.close()</code>和<code>pool.join()</code>时,我的内存问题与<a href="https://stackoverflow.com/questions/18414020/memory-usage-keep-growing-with-pythons-multiprocessing-pool">Memory usage keep growing with Python's multiprocessing.pool</a>相同。该函数运行良好,但在Win7 64计算机上没有正确地进行垃圾回收,每次调用该函数时,内存使用量都会失控,直到它使整个操作系统崩溃。下面是修复漏洞的代码:</p>
<pre><code>stringList = []
for possible_string in stringArray:
stringList.append((searchString,possible_string))
pool = Pool(5)
results = pool.map(myLevenshteinFunction, stringList)
pool.close()
pool.join()
</code></pre>
<p>关闭并加入池后,内存泄漏消失了。</p>