I'm having a strange issue in which a strange memory leak happens while reading from file in Python2.
I tried to eliminate leaking code for several hours, I failed. Then I tried to isolate leaking code and wrote a minimal program which reproduces the same leak. Here it is:
import sys
import re
@profile
def leak(filename):
residual_line = []
with open(filename) as f:
for line in f:
splitted_line = re.split(r' |\n', line)
del line
filtered_line = filter(lambda x: x != '', splitted_line)
del splitted_line
filtered_line = residual_line + filtered_line
for x in range(0,len(filtered_line)):
a=5
residual_line = filtered_line
del filtered_line
del residual_line
@profile
def main():
filename = sys.argv[1]
leak(filename)
sys.exit(0)
main()
I'm profiling it with memory_profiler module, and here's the profiling output:
As you can see, a memory alloc happens in line 8 but never releases. Leak is 31 KiB, the file I'm trying to read is 3.4kB. If I double the filesize, leak becomes 70KiB, double again 160KiB, so leak probably depends on the file.
I hope someone can find the leak, thanks in advance.

range(0, len(filtered_line)), or if you useresidual_line += filtered_lineinstead of the weird and inefficient way you're currently adding toresidual_line?withclause exits between lines 17 and 18. Maybe the profiler isn't so good a figuring that out.