|
| 1 | +.. Licensed to the Apache Software Foundation (ASF) under one |
| 2 | + or more contributor license agreements. See the NOTICE file |
| 3 | + distributed with this work for additional information |
| 4 | + regarding copyright ownership. The ASF licenses this file |
| 5 | + to you under the Apache License, Version 2.0 (the |
| 6 | + "License"); you may not use this file except in compliance |
| 7 | + with the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + .. http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + .. Unless required by applicable law or agreed to in writing, |
| 12 | + software distributed under the License is distributed on an |
| 13 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + KIND, either express or implied. See the License for the |
| 15 | + specific language governing permissions and limitations |
| 16 | + under the License. |
| 17 | +
|
| 18 | +.. _memory-profiling: |
| 19 | + |
| 20 | +Memory Profiling with Memray |
| 21 | +============================= |
| 22 | + |
| 23 | +Airflow integrates `Memray <https://bloomberg.github.io/memray/>`__, a memory profiler for Python, |
| 24 | +to help you diagnose memory usage patterns and identify potential memory leaks in Airflow components. |
| 25 | +This guide will walk you through how to profile memory usage in key Airflow components such as the |
| 26 | +scheduler, API server, and DAG processor. |
| 27 | + |
| 28 | +.. note:: |
| 29 | + |
| 30 | + Memory profiling is an expensive operation and should generally only be used for debugging purposes |
| 31 | + in development. It is not recommended for production use. |
| 32 | + See :ref:`memory-profiling-precautions` for important precautions. |
| 33 | + |
| 34 | +Prerequisites |
| 35 | +------------- |
| 36 | + |
| 37 | +Before you can use memory profiling, you need to install Airflow with the ``memray`` extra: |
| 38 | + |
| 39 | +.. code-block:: bash |
| 40 | +
|
| 41 | + pip install 'apache-airflow[memray]' |
| 42 | +
|
| 43 | +Alternatively, if you have an existing Airflow installation: |
| 44 | + |
| 45 | +.. code-block:: bash |
| 46 | +
|
| 47 | + pip install 'memray>=1.19.0' |
| 48 | +
|
| 49 | +.. note:: |
| 50 | + |
| 51 | + For more information about supported environments, see the |
| 52 | + `Memray supported environments documentation <https://bloomberg.github.io/memray/supported_environments.html>`__. |
| 53 | + |
| 54 | + |
| 55 | +Configuring Memory Profiling |
| 56 | +----------------------------- |
| 57 | + |
| 58 | +Memory profiling is controlled through Airflow's configuration. You can enable it for specific |
| 59 | +components by setting the ``memray_trace_components`` option in the ``[profiling]`` section of your |
| 60 | +``airflow.cfg`` file or through environment variables. |
| 61 | + |
| 62 | +Configuration Options |
| 63 | +^^^^^^^^^^^^^^^^^^^^^ |
| 64 | + |
| 65 | +Add the following to your ``airflow.cfg`` file: |
| 66 | + |
| 67 | +.. code-block:: ini |
| 68 | +
|
| 69 | + [profiling] |
| 70 | + # Comma-separated list of Airflow components to profile with memray |
| 71 | + # Valid components: scheduler, api, dag_processor |
| 72 | + # Invalid component names will be ignored |
| 73 | + memray_trace_components = scheduler,dag_processor,api |
| 74 | +
|
| 75 | +Or set it via environment variable: |
| 76 | + |
| 77 | +.. code-block:: bash |
| 78 | +
|
| 79 | + export AIRFLOW__PROFILING__MEMRAY_TRACE_COMPONENTS="scheduler,dag_processor,api" |
| 80 | +
|
| 81 | +.. note:: |
| 82 | + |
| 83 | + To disable memory profiling after you've completed your analysis, simply set |
| 84 | + ``memray_trace_components`` to an empty string (or unset the environment variable) |
| 85 | + and restart the affected components. |
| 86 | + |
| 87 | +Step-by-Step Profiling Guide |
| 88 | +----------------------------- |
| 89 | + |
| 90 | +This section provides a practical walkthrough of how to profile memory usage in your Airflow deployment. |
| 91 | + |
| 92 | +Step 1: Enable Memory Profiling |
| 93 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 94 | + |
| 95 | +First, decide which component you want to profile. For this example, let's profile the scheduler. |
| 96 | + |
| 97 | +Edit your ``airflow.cfg``: |
| 98 | + |
| 99 | +.. code-block:: ini |
| 100 | +
|
| 101 | + [profiling] |
| 102 | + memray_trace_components = scheduler |
| 103 | +
|
| 104 | +Or set the environment variable: |
| 105 | + |
| 106 | +.. code-block:: bash |
| 107 | +
|
| 108 | + export AIRFLOW__PROFILING__MEMRAY_TRACE_COMPONENTS=scheduler |
| 109 | +
|
| 110 | +Step 2: Restart the Component |
| 111 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 112 | + |
| 113 | +After enabling profiling for a component, you need to restart it for the changes to take effect. |
| 114 | + |
| 115 | +.. code-block:: bash |
| 116 | +
|
| 117 | + # If running standalone |
| 118 | + airflow scheduler |
| 119 | +
|
| 120 | + # If running with systemd |
| 121 | + sudo systemctl restart airflow-scheduler |
| 122 | +
|
| 123 | + # If running with Docker Compose |
| 124 | + docker-compose restart airflow-scheduler |
| 125 | +
|
| 126 | +Step 3: Run Your Workload |
| 127 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 128 | + |
| 129 | +Let Airflow run normally and perform the operations you want to profile. For example: |
| 130 | + |
| 131 | +- Let the scheduler run for a period of time |
| 132 | +- Process specific DAG files that may be problematic |
| 133 | + |
| 134 | +The longer you let it run, the more data you'll collect. However, keep in mind that memory |
| 135 | +profiling adds overhead, so a few minutes to an hour is usually sufficient for diagnosis. |
| 136 | + |
| 137 | +Step 4: Retrieve the Profile File |
| 138 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 139 | + |
| 140 | +Memray will automatically generate a binary profile file in your ``$AIRFLOW_HOME`` directory. |
| 141 | +The filename follows the pattern ``<component>_memory.bin``: |
| 142 | + |
| 143 | +.. code-block:: bash |
| 144 | +
|
| 145 | + # Default locations |
| 146 | + $AIRFLOW_HOME/scheduler_memory.bin |
| 147 | + $AIRFLOW_HOME/api_memory.bin |
| 148 | + $AIRFLOW_HOME/dag_processor_memory.bin |
| 149 | +
|
| 150 | +If running in a containerized environment, you may need to copy the file from the container: |
| 151 | + |
| 152 | +.. code-block:: bash |
| 153 | +
|
| 154 | + # Docker |
| 155 | + docker cp <container_name>:/path/to/airflow/home/scheduler_memory.bin . |
| 156 | +
|
| 157 | + # Kubernetes |
| 158 | + kubectl cp <namespace>/<pod_name>:/path/to/airflow/home/scheduler_memory.bin ./scheduler_memory.bin |
| 159 | +
|
| 160 | +Step 5: Analyze the Profile |
| 161 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 162 | + |
| 163 | +Once you have the profile file, use Memray's analysis tools to visualize and understand the memory usage. |
| 164 | + |
| 165 | +Generate a Flamegraph |
| 166 | +""""""""""""""""""""" |
| 167 | + |
| 168 | +The flamegraph is the most common way to visualize memory allocations: |
| 169 | + |
| 170 | +.. code-block:: bash |
| 171 | +
|
| 172 | + memray flamegraph scheduler_memory.bin |
| 173 | +
|
| 174 | +This will generate an HTML file (``memray-flamegraph-scheduler_memory.html``) that you can open in a web browser. |
| 175 | + |
| 176 | +.. image:: ../img/memray-flamegraph.png |
| 177 | + :alt: Example Memray flamegraph showing memory allocations |
| 178 | + |
| 179 | +The flamegraph shows the call stack with the width of each box representing the amount of memory allocated |
| 180 | +by that function. Functions at the top of the graph are leaf functions that directly allocate memory. |
| 181 | + |
| 182 | +Other Analysis Methods |
| 183 | +"""""""""""""""""""""" |
| 184 | + |
| 185 | +Memray provides several other ways to analyze memory profiles, including table reports, summary statistics, |
| 186 | +live monitoring, and more. For detailed information on all available analysis commands and options, |
| 187 | +refer to the `Memray documentation on analyzing results <https://bloomberg.github.io/memray/run.html>`__. |
| 188 | + |
| 189 | +Interpreting Results |
| 190 | +-------------------- |
| 191 | + |
| 192 | +When analyzing your memory profile, look for: |
| 193 | + |
| 194 | +**High Memory Allocation Functions** |
| 195 | + Functions that allocate large amounts of memory or are called frequently. These are the widest |
| 196 | + bars in the flamegraph or top entries in the table report. |
| 197 | + |
| 198 | +**Memory Retention Patterns** |
| 199 | + If you see certain functions consistently holding memory over time, this could indicate a memory leak. |
| 200 | + |
| 201 | +**Unexpected Allocations** |
| 202 | + Look for memory allocations in places you wouldn't expect, which might indicate inefficient code |
| 203 | + or unnecessary data structures. |
| 204 | + |
| 205 | +**Third-Party Library Usage** |
| 206 | + Sometimes memory issues are caused by how third-party libraries are used. The flamegraph will |
| 207 | + show you if a particular library is responsible for high memory usage. |
| 208 | + |
| 209 | +.. _memory-profiling-precautions: |
| 210 | + |
| 211 | +Precautions |
| 212 | +-------------- |
| 213 | + |
| 214 | +1. **Profile in Non-Production Environments** |
| 215 | + Memory profiling adds significant overhead, including increased memory usage and performance |
| 216 | + degradation. Use it in development that mirror your production setup. |
| 217 | + |
| 218 | +2. **Use Representative Workloads** |
| 219 | + Ensure the workload you're profiling is representative of your actual use case. |
| 220 | + |
| 221 | +3. **Manage Profile File Sizes** |
| 222 | + Profile files can grow very large (hundreds of MB to several GB) and may consume significant |
| 223 | + disk space. Monitor available disk space during profiling sessions and regularly clean up |
| 224 | + old profile files after analysis to prevent storage issues. |
| 225 | + |
| 226 | + |
| 227 | +Further Reading |
| 228 | +--------------- |
| 229 | + |
| 230 | +For more detailed information about Memray and its capabilities, refer to the official documentation: |
| 231 | + |
| 232 | +- `Memray Documentation <https://bloomberg.github.io/memray/>`__ |
| 233 | +- `Memray Getting Started Guide <https://bloomberg.github.io/memray/getting_started.html>`__ |
| 234 | +- `Understanding Memray Output <https://bloomberg.github.io/memray/run.html>`__ |
| 235 | +- `Memray API Reference <https://bloomberg.github.io/memray/api.html>`__ |
0 commit comments