Diagnosing and Fixing Memory Leaks in Python

Fugue uses Python extensively throughout the Conductor and in our support tools, due to its ease-of-use, extensive package library, and powerful language tools. One thing we've learned from building complex software for the cloud is that a language is only as good as its debugging and profiling tools. Logic errors, CPU spikes, and memory leaks are inevitable, but a good debugger, CPU profiler, and memory profiler can make finding these errors significantly easier and faster, letting our developers get back to creating Fugue’s dynamic cloud orchestration and enforcement system. Let’s look at a case in point.


In the fall, our metrics reported that a Python component of Fugue called the reflector was experiencing random restarts and instability after a few days of uptime. Looking at memory usage showed that the reflector's memory footprint increased monotonically and continuously, indicating a memory leak. tracemalloc, a powerful memory tracking tool in the Python standard library, made it possible to quickly diagnose and fix the leak. We discovered that the memory leak was related to our use of requests, a popular third-party Python HTTP library. Rewriting the component to use urllib from the Python standard library eliminated the memory leak. In this blog, we'll explore the details.


Metrics show the problem: Percentage of total system memory used by the reflector, using the requests library.


Memory Allocation in Python


In most scenarios, there's no need to understand memory management in Python beyond knowing that the interpreter manages memory for you. However, when writing large, complex Python programs with high stability requirements, it’s useful to peek behind the curtain to understand how to write code that interacts well with Python's memory management algorithms. Notably, Python uses reference counting and garbage collection to free memory blocks, and only frees memory to the system when certain internal requirements are met. A pure Python script will never have direct control over memory allocation in the interpreter. If direct control over memory allocation is desired, the interpreter's memory allocation can be bypassed by writing or using an extension. For example, numpy manages memory for large data arrays using its own memory allocator.


Fundamentally, Python is a garbage-collected language that uses reference counting. The interpreter automatically allocates memory for objects as they are created and tracks the number of references to those objects in a data structure associated with the object itself. This memory will be freed when the reference count for those objects reaches zero. In addition, garbage collection will detect cycles and remove objects that are only referenced in cycles. Every byte of memory allocated within the Python interpreter is able to be freed between these two mechanisms, but no claims can be made about memory allocated in extensions.


Python manages its own heap, separate from the system heap. Memory is allocated in the Python interpreter by different methods according to the type of the object to be created. Scalar types, such as integers and floats, use different memory allocation methods than composite types, such as lists, tuples, and dictionaries. In general, memory is allocated on the Python heap in fixed-size blocks, depending on the type. These blocks are organized into pools, which are further organized into arenas. Memory is pre-allocated using arenas, pools, and blocks, which are then used to store data as needed over the course of program’s execution. Since these blocks, pools, and arenas are kept in Python's own heap, freeing a memory block merely marks it as available for future use in the interpreter. Freeing memory in Python does not immediately free the memory at the system level. When an entire arena is marked as free, its memory is released by the Python interpreter and returned to the system. However, this may occur infrequently due to memory fragmentation.


Due to these abstractions, memory usage in Python often exhibits high-water-mark behavior, where peak memory usage determines the memory usage for the remainder of execution, regardless of whether that memory is actively being used. Furthermore, the relationship between memory being "freed" in code and being returned to the system is vague and difficult to predict. These behaviors make completely understanding the memory usage of complex Python programs notoriously difficult.


Memory Profiling Using tracemalloc


tracemalloc is a package included in the Python standard library (as of version 3.4). It provides detailed, block-level traces of memory allocation, including the full traceback to the line where the memory allocation occurred, and statistics for the overall memory behavior of a program. The documentation is available here and provides a good introduction to its capabilities. The original Python Enhancement Proposal (PEP) introducing it also has some insight on its design.


tracemalloc can be used to locate high-memory-usage areas of code in two ways:


  • looking at cumulative statistics on memory use to identify which object allocations are using the most memory, and
  • tracing execution frames to identify where those objects are allocated in the code.


Module-level Memory Usage


We start by tracing the memory usage of the entire program, so we can identify, at a high level, which objects are using the most memory. This will hopefully provide us with enough insight to know where and how to look more deeply. The following wrapper starts tracing and prints statistics when Ctrl-C is hit:

<code>import tracemalloctracemalloc.<span class="hljs-function"><span class="hljs-title">start</span><span class="hljs-params">(<span class="hljs-number">10</span>)</span></span>try:    <span class="hljs-function"><span class="hljs-title">run_reflector</span><span class="hljs-params">()</span></span>except:    snapshot = tracemalloc.<span class="hljs-function"><span class="hljs-title">take_snapshot</span><span class="hljs-params">()</span></span>    <span class="hljs-function"><span class="hljs-title">top_n</span><span class="hljs-params">(<span class="hljs-number">25</span>, snapshot, trace_type=<span class="hljs-string">'filename'</span>)</span></span></code>

tracemalloc.start(10) starts memory tracing, while saving 10 frames of traceback for each entry. The default is 1, but saving more traceback frames is useful if you plan on using tracebacks to locate memory leaks, which will be discussed later. tracemalloc.take_snapshot() takes a snapshot of currently allocated memory in the Python heap. It stores the number of allocated blocks, their size, and tracebacks to identify which lines of code allocated which blocks of memory. Once a snapshot is created, we can compute statistics on memory use, compare snapshots, or save them to analyze later. top_n is a helper function I wrote to pretty print the output from tracemalloc. Here, I ask for the top 25 memory allocations in the snapshot, grouped by filename. After running for a few minutes, the output looks like this:

<code>[ Top <span class="hljs-number">25</span> with filename tracebacks ]<span class="hljs-number">197618</span> blocks <span class="hljs-number">17.02311134338379</span> MB/Users/mike/.pyenv/versions/<span class="hljs-number">3.4</span><span class="hljs-number">.2</span>/lib/python3<span class="hljs-number">.4</span>/collections/__init__.py:<span class="hljs-number">0</span>: size=<span class="hljs-number">17.0</span> MiB, count=<span class="hljs-number">197618</span>, average=<span class="hljs-number">90</span> B<span class="hljs-number">105364</span> blocks <span class="hljs-number">11.34091567993164</span> MB&lt;frozen importlib._bootstrap&gt;:<span class="hljs-number">0</span>: size=<span class="hljs-number">11.3</span> MiB, count=<span class="hljs-number">105364</span>, average=<span class="hljs-number">113</span> B<span class="hljs-number">60339</span> blocks <span class="hljs-number">9.233230590820312</span> MB/Users/mike/.pyenv/versions/<span class="hljs-number">3.4</span><span class="hljs-number">.2</span>/lib/python3<span class="hljs-number">.4</span>/json/decoder.py:<span class="hljs-number">0</span>: size=<span class="hljs-number">9455</span> KiB, count=<span class="hljs-number">60339</span>, average=<span class="hljs-number">160</span> B...</code>

This shows the cumulative amount of memory allocated by the component over the entire runtime, grouped by filename. At this level of granularity, it's hard to make sense of the results. For instance, the first line shows us that 17 MB of collections objects are created, but this view doesn't provide enough detail for us to know which objects, or where they're being used. A different approach is needed to isolate the problem.


Understanding tracemalloc Output


tracemalloc shows the net memory usage at the time a memory snapshot is taken. When comparing two snapshots, it shows the net memory usage between the two snapshots. If memory is allocated and freed between snapshots, it won't be shown in the output. Therefore, if snapshots are created at the same point in a loop, any memory allocations visible in the differences between two snapshots are contributing to the long-term total amount of memory used, rather than being a temporary allocation made in the course of execution.


In the case of reference cycles that require garbage collection, uncollected cycles are recorded in the output, while collected cycles are not. Any blocks freed by the garbage collector in the time covered by a snapshot will be recorded as freed memory. Therefore, forcing garbage collection with gc.collect() before taking a snapshot will reduce noise in the output.


Per-Iteration Memory Usage


Since we're looking for a memory leak, it's useful to understand how the memory usage of our program changes over time. We can instrument the main loop of the component, to see how much memory is allocated in each iteration, by calling the following method from the main loop:

<code>   def <span class="hljs-function"><span class="hljs-title">collect_stats</span><span class="hljs-params">(self)</span></span>:        self<span class="hljs-class">.snapshots</span><span class="hljs-class">.append</span>(tracemalloc.<span class="hljs-function"><span class="hljs-title">take_snapshot</span><span class="hljs-params">()</span></span>)        <span class="hljs-keyword">if</span> <span class="hljs-function"><span class="hljs-title">len</span><span class="hljs-params">(self.snapshots)</span></span> &gt; <span class="hljs-number">1</span>:            stats = self<span class="hljs-class">.snapshots</span>[-<span class="hljs-number">1</span>].<span class="hljs-function"><span class="hljs-title">filter_traces</span><span class="hljs-params">(filters)</span></span>.<span class="hljs-function"><span class="hljs-title">compare_to</span><span class="hljs-params">(self.snapshots[-<span class="hljs-number">2</span>], <span class="hljs-string">'filename'</span>)</span></span>            <span class="hljs-keyword">for</span> stat <span class="hljs-keyword">in</span> stats[:<span class="hljs-number">10</span>]:                <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(<span class="hljs-string">"{} new KiB {} total KiB {} new {} total memory blocks: "</span>.format(stat.size_diff/<span class="hljs-number">1024</span>, stat.size / <span class="hljs-number">1024</span>, stat.count_diff ,stat.count)</span></span>)                <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> stat<span class="hljs-class">.traceback</span><span class="hljs-class">.format</span>():                    <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(line)</span></span></code>

This code takes a memory snapshot and saves it, then uses snapshot.compare_to(other_snapshot, group_by='filename') to compare the newest snapshot with the previous snapshot, with results grouped by filename. After a few iterations to warm up memory, the output looks like this:

<code>[ Top <span class="hljs-number">5</span> with filename tracebacks ]<span class="hljs-number">190.7421875</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">1356.5634765625</span> total KiB <span class="hljs-number">1930</span> <span class="hljs-keyword">new</span> <span class="hljs-number">13574</span> total memory blocks:      (<span class="hljs-number">1</span>)  File <span class="hljs-string">"/Users/mike/.pyenv/versions/3.4.2/lib/python3.4/linecache.py"</span>, line <span class="hljs-number">0</span><span class="hljs-number">2.1328125</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">12.375</span> total KiB <span class="hljs-number">32</span> <span class="hljs-keyword">new</span> <span class="hljs-number">86</span> total memory blocks:             (<span class="hljs-number">2</span>)  File <span class="hljs-string">"/Users/mike/.pyenv/versions/3.4.2/lib/python3.4/tracemalloc.py"</span>, line <span class="hljs-number">0</span><span class="hljs-number">1.859375</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">18.7001953125</span> total KiB <span class="hljs-number">3</span> <span class="hljs-keyword">new</span> <span class="hljs-number">53</span> total memory blocks:         (<span class="hljs-number">3</span>)  File <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/packages/urllib3/connection.py"</span>, line <span class="hljs-number">0</span>-<span class="hljs-number">1.71875</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">34.5224609375</span> total KiB -<span class="hljs-number">2</span> <span class="hljs-keyword">new</span> <span class="hljs-number">91</span> total memory blocks:   File <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py"</span>, line <span class="hljs-number">0</span><span class="hljs-number">1.66015625</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">61.662109375</span> total KiB <span class="hljs-number">18</span> <span class="hljs-keyword">new</span> <span class="hljs-number">260</span> total memory blocks:   File <span class="hljs-string">"/Users/mike/.pyenv/versions/3.4.2/lib/python3.4/urllib/parse.py"</span>, line <span class="hljs-number">0</span></code>

The linecache (1) and tracemalloc (2) allocations are part of the instrumentation, but we can also see some memory allocations made by the requests HTTP package (3) that warrant further investigation. Recall that tracemalloc tracks net memory usage, so these memory allocations are accumulating on each iteration. Although the individual allocations are small and don't jump out as problematic, the memory leak only becomes apparent over the course of a few days, so it's likely to be a case of small losses adding up.


Filtering Snapshots


Now that we have an idea of where to look, we can use tracemalloc's filtering capabilities to show only memory allocations related to the requests package:

<code>   from tracemalloc import Filter    filters = [<span class="hljs-function"><span class="hljs-title">Filter</span><span class="hljs-params">(inclusive=True, filename_pattern=<span class="hljs-string">"*requests*"</span>)</span></span>]    filtered_stats = snapshot.<span class="hljs-function"><span class="hljs-title">filter_traces</span><span class="hljs-params">(filters)</span></span>.<span class="hljs-function"><span class="hljs-title">compare_to</span><span class="hljs-params">(old_snapshot.filter_traces(filters)</span></span>, <span class="hljs-string">'traceback'</span>)    <span class="hljs-keyword">for</span> stat <span class="hljs-keyword">in</span> stats[:<span class="hljs-number">10</span>]:        <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(<span class="hljs-string">"{} new KiB {} total KiB {} new {} total memory blocks: "</span>.format(stat.size_diff/<span class="hljs-number">1024</span>, stat.size / <span class="hljs-number">1024</span>, stat.count_diff ,stat.count)</span></span>)        <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> stat<span class="hljs-class">.traceback</span><span class="hljs-class">.format</span>():            <span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(line)</span></span></code>

snapshot.filter_traces() takes a list of Filters to apply to the snapshot. Here, we create a Filter in inclusive mode, so it includes only traces that match the filename_pattern. When inclusive is False, the filter excludes traces that match the filename_pattern. The filename_pattern uses UNIX-style wildcards to match filenames in the traceback. In this example, the wildcards in "requests" match occurrences of "requests" in the middle of a path, such as "/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/sessions.py".


We then use compare_to() to compare the results to the previous snapshot. The filtered output is below:

<code><span class="hljs-number">48.7890625</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">373.974609375</span> total KiB <span class="hljs-number">4</span> <span class="hljs-keyword">new</span> <span class="hljs-number">1440</span> total memory blocks:                 (<span class="hljs-number">4</span>)  File <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/structures.py"</span>, line <span class="hljs-number">0</span><span class="hljs-number">1.46875</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">16.2939453125</span> total KiB <span class="hljs-number">2</span> <span class="hljs-keyword">new</span> <span class="hljs-number">49</span> total memory blocks:   File <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests_unixsocket/__init__.py"</span>, line <span class="hljs-number">0</span>    -<span class="hljs-number">1.4453125</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">34.2802734375</span> total KiB -<span class="hljs-number">2</span> <span class="hljs-keyword">new</span> <span class="hljs-number">96</span> total memory blocks:                 (<span class="hljs-number">5</span>)  File <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/sessions.py"</span>, line <span class="hljs-number">0</span>-<span class="hljs-number">0.859375</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">31.8505859375</span> total KiB -<span class="hljs-number">1</span> <span class="hljs-keyword">new</span> <span class="hljs-number">85</span> total memory blocks:   File <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py"</span>, line <span class="hljs-number">0</span><span class="hljs-number">0.6484375</span> <span class="hljs-keyword">new</span> KiB <span class="hljs-number">20.8330078125</span> total KiB <span class="hljs-number">1</span> <span class="hljs-keyword">new</span> <span class="hljs-number">56</span> total memory blocks:   File <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/packages/urllib3/connection.py"</span>, line <span class="hljs-number">0</span></code>

With the Filter in place, we can clearly see how requests is using memory. Line (4) shows that roughly 50 KiB of memory is lost in requests on each iteration of the main loop. Note that negative memory allocations, such as (5), are visible in this output. These allocations are freeing memory allocated in previous loop iterations.


Tracking Down Memory Allocations


To determine which uses of requests are leaking memory, we can take a detailed look at where problematic memory allocations occur by calling compare_to() with traceback instead of filename, while using a Filter to narrow down the output:

<code>   stats = snapshot.<span class="hljs-function"><span class="hljs-title">filter_traces</span><span class="hljs-params">(filters)</span></span>.<span class="hljs-function"><span class="hljs-title">compare_to</span><span class="hljs-params">(old_snapshot.filter_traces(filters)</span></span>, <span class="hljs-string">'traceback'</span>)</code>

This prints 10 frames of traceback (since we started tracing with tracemalloc.start(10)) for each entry in the output, a truncated example of which is below:

<code><span class="hljs-number">5</span> memory blocks: <span class="hljs-number">4</span>.<span class="hljs-number">4921875</span> <span class="hljs-type">KiB</span>  <span class="hljs-type">File</span> <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/sessions.py"</span>, line <span class="hljs-number">585</span>    r = adapter.send(request, **kwargs)  <span class="hljs-type">File</span> <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests/sessions.py"</span>, line <span class="hljs-number">475</span>    resp = self.send(prep, **send_kwargs)  <span class="hljs-type">File</span> <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests_unixsocket/__init__.py"</span>, line <span class="hljs-number">46</span>    <span class="hljs-keyword">return</span> session.request(<span class="hljs-keyword">method</span>=<span class="hljs-keyword">method</span>, url=url, **kwargs)  <span class="hljs-type">File</span> <span class="hljs-string">"/Users/mike/.pyenv/versions/venv/lib/python3.4/site-packages/requests_unixsocket/__init__.py"</span>, line <span class="hljs-number">60</span>    <span class="hljs-keyword">return</span> request('post', url, data=data, json=json, **kwargs)</code>

The full traceback gives us the ability to trace backwards from memory allocations to the lines in our project code that generate them. In the case of this component, our uses of requests came from an internal storage library that used an HTTP API. Rewriting the library to use urllib directly eliminated the memory leak.

Metrics indicate the problem is solved: Percentage of total system memory used by the reflector, after removing requests and switching to urllib.


Memory Profiling: Art or Science?


tracemalloc is a powerful tool for understanding the memory usage of Python programs. It helped us understand module-level memory usage, find out which objects are being allocated the most, and it demonstrated how the reflector’s memory usage changed on a per-iteration basis. It comes with useful filtering tools and gives us the ability to see the full traceback for any memory allocation. Despite all of its features, however, finding memory leaks in Python can still feel like more of an art than a science. Memory profilers give us the ability to see how memory is being used, but oftentimes it’s difficult to find the exact memory allocation that is causing problems. It’s up to us to synthesize the information we get from our tools into a conclusion about the memory behavior of the program, then make a decision about what actions to take from there.


We use virtually every available Python tool (test frameworks, cProfile, etc.) to make Fugue’s system reliable, performant, and easy to maintain. The broker and reflector both take advantage of Python's introspection to make judgments about dynamic calls to the AWS API, which allows us to focus on logic rather than coding exhaustive cases. Fugue leverages the strengths of Python where it makes sense in the system, which ultimately means more product stability and extensibility for end-users.


Categorized Under

Fugue Productivity Programming

Secure Your Cloud

Find security and compliance violations in your cloud infrastructure and ensure they never happen again.