'Recursive method causing memory leak
I was stumbled upon finding this function leaking after running tracemalloc on my application;
By running tracemalloc.take_snapshot().statistics('traceback'), within the first line I could find a traceback leading to this function:
def sanitize_nested_object(value):
# Remove unicode character that can not be save to database as text
invalid_unicode = '\x00'
if type(value) == dict:
return {
sanitize_nested_object(k): sanitize_nested_object(v) # <-- This line
for k, v in value.items()
}
if type(value) == list:
return [sanitize_nested_object(el) for el in value]
if type(value) == str:
return value.replace(invalid_unicode, '')
return value
With trace statistics of size=5050992 and count=678 it seems traceback.format() points to this particular line: sanitize_nested_object(k): sanitize_nested_object(v).
I am actually unsure on how to pursue investigating, and I'm thinking of wether the leak comes from the recursion, the in-line dictionary, or array generation.
What could be some next steps to debug the leak, and if this function is actually leaking, what could make it do so?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
