Skip to content

Memory Leak with Pandas read_parquet using pyarrow engine #45504

Closed as duplicate

Description

@twkim112

I’ve encountered a memory issue when reading Parquet files with Pandas using the pyarrow engine. Even though pyarrow.total_allocated_bytes() reports that allocated memory goes back to zero after each function call, the overall process memory (as reported by psutil) keeps increasing significantly over repeated calls.

Steps to Reproduce:

import psutil
import time
import pandas as pd
import gc
import pyarrow as pa

# pa.jemalloc_set_decay_ms(0)

def print_memory_usage():
    process = psutil.Process()
    mem_info = process.memory_info()
    print(f"PA allocated_bytes after function call: {pa.total_allocated_bytes() / 1024 / 1024:.2f} MB")
    print(f"Memory Usage: {mem_info.rss / 1024 / 1024:.2f} MB")

def mem_and_time(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()

        # Print results
        print_memory_usage()
        print(f"Execution Time: {end_time - start_time:.6f} seconds")
        return result
    return wrapper

@mem_and_time
def test_func_pandas():
    # When using fastparquet, the memory usage is stable:
    # df = pd.read_parquet("/Users/test.parquet", engine='fastparquet')
    df = pd.read_parquet("/Users/test.parquet", engine='pyarrow')
    print(f"PA allocated_bytes inside function call: {pa.total_allocated_bytes() / 1024 / 1024:.2f} MB")
    return None

if __name__ == "__main__":
    for _ in range(10000):
        test_func_pandas()

Observe that:

Inside each function call, pyarrow.total_allocated_bytes() reports a large allocation (e.g., ~2646 MB).
After the function call, pyarrow.total_allocated_bytes() resets to 0 MB.
However, the overall process memory usage (as shown by psutil) increases with each iteration.

PA allocated_bytes inside function call: 2646.27 MB
PA allocated_bytes after function call: 0.00 MB
Memory Usage: 3147.12 MB
Execution Time: 0.669164 seconds
PA allocated_bytes inside function call: 2646.27 MB
PA allocated_bytes after function call: 0.00 MB
Memory Usage: 3945.00 MB
Execution Time: 0.623360 seconds
PA allocated_bytes inside function call: 2646.27 MB
PA allocated_bytes after function call: 0.00 MB
Memory Usage: 4494.80 MB
Execution Time: 0.681895 seconds
PA allocated_bytes inside function call: 2646.27 MB
PA allocated_bytes after function call: 0.00 MB
Memory Usage: 4865.27 MB
Execution Time: 0.641056 seconds

...

PA allocated_bytes inside function call: 2646.27 MB
PA allocated_bytes after function call: 0.00 MB
Memory Usage: 6157.64 MB
Execution Time: 0.659480 seconds

Environment:

Python: 3.13.1
Pandas: 2.2.3
PyArrow: 19.0.0
OS: MacOs Sequoia 15.3.1

Component(s)

Python

Activity

  1. erykoff commented on Mar 19, 2025

    @erykoff

    I have encountered what I think is the same major memory leak when reading large parquet files with pyarrow.parquet.read_table(), no pandas even installed. pyarrow.total_allocated_bytes() reports constant numbers, but the actual memory usage just goes up and up and up.

    Whatever the problem is appeared between pyarrow=17 (which uses the expected amount of memory and doesn't leak) and pyarrow=18 (which leaks like a sieve).

    This is a major regression.

  2. twkim112 commented on Mar 24, 2025

    @twkim112
    Author

    @erykoff how do you handle this issue, by using the fastparquet engine or downgrading to version 17?

  3. erykoff commented on Mar 24, 2025

    @erykoff

    We use parquet without pandas, so fastparquet isn't an option. Currently pinning back to v17. I'm also trying to come up with a standalone repro for a better bug report.

  4. mthiboust commented on Apr 8, 2025

    @mthiboust

    I have the same issue with pyarrow v18 and v19, but not with v17 as described here: #45882 (comment)

  5. mthiboust commented on Apr 9, 2025

    @mthiboust

    After some investigation, it appears that the difference between versions in the memory profile above is explained by the choice of memory allocator: jemalloc by default for v17 vs mimalloc for v18 and v19.

    Below is a new memory profile with pyarrow==19.0.1 for mimalloc on the left and jemalloc on the right (loop of 5 iteration of parquet file reading).

    Image

    You can set jemalloc as the default memory allocator using: export ARROW_DEFAULT_MEMORY_POOL=jemalloc

    See:

    The default memory pool has changed to mimalloc on all platforms (GH-43254). Previously, jemalloc was used by default on Linux. Using mimalloc by default provides a more consistent experience across different platforms, and makes configuration easier. It is expected that this might either increase or decrease performance on user workloads that use the default memory pool; please benchmark accordingly. Jemalloc can still be selected by setting the ARROW_DEFAULT_MEMORY_POOL environment variable to “jemalloc”.
    https://arrow.apache.org/blog/2024/10/28/18.0.0-release/

  6. antareson commented on Jun 1, 2026

    @antareson

    Is this fixed or still an issue in version 24?

  7. Prussian1870 commented on Jun 2, 2026

    @Prussian1870

    I'm having this issue using the CompressedInputStream.

    Environment:

    Python: 3.10
    PyArrow: 21.0.0
    OS: windows 10 pro

  8. erykoff commented on Jun 2, 2026

    @erykoff

    I have checked with the latest v24 pyarrow and the default memory allocator still fails to release memory. It's possible that it only uses 3x the memory it should rather than growing unbounded. But overriding ARROW_DEFAULT_MEMORY_POOL=jemalloc uses 3x less memory on my large test file and doesn't grow at all (as expected and desired).

  9. Prussian1870 commented on Jun 6, 2026

    @Prussian1870

    Thanks for the help. But that didn't work in python 3.10:

    os.environ["ARROW_DEFAULT_MEMORY_POOL"]="jemalloc"

    I assume the setting can be set at runtime.

    Correct?

    In the meantime I started using DataFusion v53 and it worked well with no performance degradation and minimal memory usage.

  10. AlenkaF commented on Jul 1, 2026

    @AlenkaF
    Member

    There are quite some issues opened on memory leak/memory allocation connected to Arrow and Parquet. I think the best thread to help one understand the topic and to keep open is #44599. I will close others as duplicate. Feel free to comment and I will re-open if we see I was mistaken and the issues are different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions