Repository navigation
Description
I have a ~1.5TiB, ~1.7k files parquet dataset with an additional _metadata.parquet file containing metadata of all row groups. The _metadata file was written with the mechanism described in the documentation.
The _metadata file is ~390MiB, the 1.7k parquet files are around 900MiB each.
I have a script called read_metadata.py which can be used to iterate through all files in the dataset, get their metadata and simultaneously measure memory load (RSS):
import gc
import time
import json
from contextlib import contextmanager
from pathlib import Path
import psutil
import pyarrow
import pyarrow.parquet
process = psutil.Process()
@contextmanager
def profiling(name: str):
start = time.monotonic()
start_mem = process.memory_info().rss / 1024**2
yield
end = time.monotonic()
end_mem = process.memory_info().rss / 1024**2
duration = end - start
if end_mem - start_mem == 0:
return
print(
f"{name}\n"
f" took {duration:.5f} s, "
f"mem diff {end_mem - start_mem:.3f}MiB [start: {start_mem:.3f}MiB, end: {end_mem:.3f}MiB]"
)
def read_metadata(path: Path) -> None:
pyarrow.parquet.read_metadata(path)
return
if __name__ == "__main__":
import argparse
import random
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+")
parser.add_argument("repeats", type=int)
args = parser.parse_args()
paths = args.files
repeats = args.repeats
paths = paths * repeats
random.shuffle(paths)
for path in paths:
with profiling(f"load: {path}"):
read_metadata(path)
with profiling(f"gc: {path}"):
gc.collect()
Doing that gives these results (note that only steps where there is a change in memory load are printed):
$ python scripts/read_metadata.py repartition/* 3
load: repartition/part-52.parquet
took 0.00132 s, mem diff 1.500MiB [start: 194.281MiB, end: 195.781MiB]
load: repartition/_metadata.parquet
took 2.38347 s, mem diff 2082.062MiB [start: 195.781MiB, end: 2277.844MiB]
load: repartition/_metadata.parquet
took 2.15518 s, mem diff 16.094MiB [start: 2277.844MiB, end: 2293.938MiB]
load: repartition/part-1587.parquet
took 0.00099 s, mem diff 1.500MiB [start: 2293.938MiB, end: 2295.438MiB]
gc: repartition/part-1299.parquet
took 0.00217 s, mem diff -1.832MiB [start: 2295.438MiB, end: 2293.605MiB]
load: repartition/_metadata.parquet
took 2.15186 s, mem diff 0.562MiB [start: 2293.605MiB, end: 2294.168MiB]
gc: repartition/part-1112.parquet
took 0.00220 s, mem diff -1.543MiB [start: 2294.168MiB, end: 2292.625MiB]
load: repartition/part-751.parquet
took 0.00111 s, mem diff 1.500MiB [start: 2292.625MiB, end: 2294.125MiB]
load: repartition/part-321.parquet
took 0.00113 s, mem diff 1.500MiB [start: 2294.125MiB, end: 2295.625MiB]
Memory load stays mostly constant, but as soon as the metadata.parquet file is read, a huge, over 2GiB large memory leak appears. Reading that particular file multiple times does not lead to multiple memory leaks.
There seems to be no way to reduce memory load back to normal levels again, not even a pool = pyarrow.default_memory_pool(); pool.release_unused() or gc.collect() does help.
Component(s)
Parquet, Python

Activity
[-]>2GiB Memory Leak on reading single parquet metadata file[/-][+][Parquet] >2GiB Memory Leak on reading single parquet metadata file[/+]pitrou commentedon Nov 12, 2024
Thanks for the report @jonded94 . Two questions:
jonasdedden commentedon Nov 14, 2024
"system" memory pool:
=> also ~2.3GiB leak at first read, but a second read even increases it to ~2.7GiB.
"jemalloc" memory pool:
"mimalloc" memory pool:
Then I tuned a bit the statment where I previously just called
gc.collect()and added a memory pool cleanup:Following datapoints resulted with this tuned version of the script.
"system" memory pool:
=> Here, the memory actually seems to be released to the system again!
"jemalloc" memory pool:
=> jemalloc memory pool doesn't care for the explicit cleanup call apparently.
"mimalloc" memory pool (this was increasing & releasing memory on every iteration and spamming stdout, I had to limit output to cases where memory changed by more than 5MiB):
=> Memory leak also still seems to appear.
pitrou commentedon Nov 14, 2024
Ok, so what this tell us is that there is no actual memory leak in Arrow (at least on this use case :-)), but the memory allocator may decide to hold onto some memory to make further allocations faster.
Note that the memory is not necessarily unavailable to other applications. For example, the memory allocator might have set these memory areas as
MADV_FREE, which lets the kernel free the pages when it needs to, but not necessarily immediately.This all shows how complicated it is to get accurate memory footprint measurements on modern OSes...
jonasdedden commentedon Nov 15, 2024
Our use case actually would requires that the workloads require as little memory as possible and also hand it back over to the system as soon as possible. If reading in a big file suddenly keeps all processes at ~2GiB more memory than they ideally would need, this is somewhat of a dealbreaker.
That all memory pools per default don't hand memory which is no longer required back to the system certainly could be considered unexpected by some. That for some reason, only the
systemmemory pool seems to react to.release_unused()calls, could be even considered a bug? It's unclear for me as a user how I would make sure to explicitly free that unused memory.pitrou commentedon Nov 15, 2024
As I said, RSS can be misleading as it shows some memory to be in use even though the kernel might be able to reuse it if desired (the
MADV_FREEcase I linked to).I'm not saying this is exactly what happens here, but it's quite likely given that the system allocator returns the memory more eagerly.
One way to clear this up would be: run this reproducer and keep the Python process running (for example using
time.sleep(3600)at the end). Then start another process that tries to use all memory, and see if the Python process' RSS finally shrinks.You may also want to open a question on the https://github.com/microsoft/mimalloc issue tracker, they'll probably be able to give more precise insight. Also, you could experiment with some of the environment variables here: https://microsoft.github.io/mimalloc/environment.html
jonasdedden commentedon Nov 15, 2024
We have a script that launches quite a lot of Python processes that use something very similar to the test script that I've shown. Unfortunately, even with 250GiB RAM available on the specific host, we see OOM errors after a handful iterations, so it appears that the memory is not entirely free to use or at least the kernel is getting uneasy.
We then replaced the very few used
pyarrowmethods with a custom in-house written, very shallow PyO3 wrapper around the parquet Rust crate which offers similar functionality. With that, we see constant memory load, regardless of long the script was run, and we're using only a few dozen GiBs total; that's far less than thepyarrowimplementation.I know that our use case is pretty specific, but I wanted to share our experience regardless. If my experience is too vague to be of any actual debugging value to you, we can close the issue.
pitrou commentedon Nov 15, 2024
Ok, I think we're talking about two separate issues here:
As I said, I think the second issue is unrelated to PyArrow, and you can probably mitigate it either by changing the default memory pool (e.g. switch to "system" if it doesn't reduce performance for you) or try to tune memory allocator options (for example by experimenting with the mimalloc environment variables I linked to).
However, the first issue may perhaps deserve improving in PyArrow. For that, it would be nice if you could compare the memory consumption with PyArrow and with parquet-rs. Also, hopefully you can upload a reproducer file for us to look at?
jonasdedden commentedon Nov 15, 2024
As all of this is unfortunately an internal project, I'm unable to share any specifics, especially files. I'm very sorry!
Maybe I can find some time to create synthetic files which show a similar behaviour.
I can do that with a similar workload that does not read metadata, but actual rows. This unfortunately is not strictly connected to the original issue mentioned here, I'm sorry! But it nevertheless could be interesting if I share our results of that. Sorry that this issue is so meandering.
This workload just consumed
RecordBatchinstances of size 2250 withpyarrow.Dataset.Fragment.to_batches(https://arrow.apache.org/docs/python/generated/pyarrow.dataset.Fragment.html#pyarrow.dataset.Fragment.to_batches). Nothing was done with them. They were just immediately discarded, very similar to the original script I shared.As one can see, memory is just steadily increasing. Running this script in parallel will very easily lead to OOM errors (which we also could reproduce).
Then we tried creating an entirely new memory pool for every data iteration as a workaround:

This helped, but the memory load still is quite high.
When using the Rust reader for the same workload, memory load seemed to behave much more predictibly and was quite a bit lower:

pitrou commentedon Nov 18, 2024
Or perhaps you can use the "parquet-dump-footer" executable here, it has an option to scrub any sensitive information from the metadata.
pitrou commentedon Feb 11, 2025
PR #37630 , which was just merged, might improve things significantly here (if you switch the new option to disable metadata caching).