Execute the function and try to detect a clear memory leak either internal to Arrow or caused by a reference counting problem in the Python binding implementation. Raises exception if a leak detected Parameters ---------- f : callable Function to invoke on each iter
(f, metric='rss', threshold=1 << 17, iterations=10,
check_interval=1)
| 122 | |
| 123 | |
| 124 | def memory_leak_check(f, metric='rss', threshold=1 << 17, iterations=10, |
| 125 | check_interval=1): |
| 126 | """ |
| 127 | Execute the function and try to detect a clear memory leak either internal |
| 128 | to Arrow or caused by a reference counting problem in the Python binding |
| 129 | implementation. Raises exception if a leak detected |
| 130 | |
| 131 | Parameters |
| 132 | ---------- |
| 133 | f : callable |
| 134 | Function to invoke on each iteration |
| 135 | metric : {'rss', 'vms', 'shared'}, default 'rss' |
| 136 | Attribute of psutil.Process.memory_info to use for determining current |
| 137 | memory use |
| 138 | threshold : int, default 128K |
| 139 | Threshold in number of bytes to consider a leak |
| 140 | iterations : int, default 10 |
| 141 | Total number of invocations of f |
| 142 | check_interval : int, default 1 |
| 143 | Number of invocations of f in between each memory use check |
| 144 | """ |
| 145 | import psutil |
| 146 | proc = psutil.Process() |
| 147 | |
| 148 | def _get_use(): |
| 149 | gc.collect() |
| 150 | return getattr(proc.memory_info(), metric) |
| 151 | |
| 152 | baseline_use = _get_use() |
| 153 | |
| 154 | def _leak_check(): |
| 155 | current_use = _get_use() |
| 156 | if current_use - baseline_use > threshold: |
| 157 | raise Exception( |
| 158 | "Memory leak detected. Departure from baseline " |
| 159 | f"{current_use - baseline_use} after {i} iterations" |
| 160 | ) |
| 161 | |
| 162 | for i in range(iterations): |
| 163 | f() |
| 164 | if i % check_interval == 0: |
| 165 | _leak_check() |
| 166 | |
| 167 | |
| 168 | def get_modified_env_with_pythonpath(): |
nothing calls this directly
no test coverage detected