Skip to content

[Python] Cannot release memory after reading a parquet file from S3 #45882

Closed as duplicate

Description

@kopczynski-9livesdata

Describe the bug, including details regarding any error messages, version, and platform.

When running a script like the following

import pyarrow.dataset as ds
import pyarrow.fs
from memory_profiler import profile
import pyarrow

@profile
def load_parquet():
    print(f"{pyarrow.total_allocated_bytes() / (1024*1024)}")
    fs = pyarrow.fs.S3FileSystem()
    s3_dataset = ds.dataset("[my_s3_bucket]/10g.parquet", filesystem=fs)
    scanner = s3_dataset.scanner()
    scanner.head(10)
    del scanner
    del s3_dataset
    del fs
    pyarrow.default_memory_pool().release_unused()
    print(f"{pyarrow.total_allocated_bytes() / (1024*1024)}")

if __name__ == "__main__":
    load_parquet()

I keep getting significant amount of memory allocated which I cannot force pyarrow to release back to OS.

Memory profile and output from this script looks like this:

0.0
1516.7496948242188
Filename: src/mem.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     6    125.2 MiB    125.2 MiB           1   @profile
     7                                         def load_parquet():
     8    125.2 MiB      0.0 MiB           1       print(f"{pyarrow.total_allocated_bytes() / (1024*1024)}")
     9    130.2 MiB      5.0 MiB           1       fs = pyarrow.fs.S3FileSystem()
    10    140.7 MiB     10.5 MiB           1       s3_dataset = ds.dataset("dotdata-ddent-tkopczynski-dev/10g.parquet", filesystem=fs)
    11    140.9 MiB      0.1 MiB           1       scanner = s3_dataset.scanner()
    12   1700.1 MiB   1559.2 MiB           1       scanner.head(10)
    13   1701.0 MiB      0.9 MiB           1       del scanner
    14   1701.5 MiB      0.5 MiB           1       del s3_dataset
    15   1701.6 MiB      0.1 MiB           1       del fs
    16   1701.3 MiB     -0.3 MiB           1       pyarrow.default_memory_pool().release_unused()
    17   1701.3 MiB      0.0 MiB           1       print(f"{pyarrow.total_allocated_bytes() / (1024*1024)}")

Is there anything I'm doing wrong or is this a memory leak?

OS: Ubuntu 22.04.5 LTS
pyarrow version: 19.0.1

Component(s)

Python

Activity

  1. mthiboust commented on Apr 7, 2025

    @mthiboust

    Same behavior here. Not sure if memory-profiler is able to correclty track memory allocated by pyarrow

  2. mthiboust commented on Apr 8, 2025

    @mthiboust

    This behavior happens with pyarrow v18 and v19 but not v17. See this memory profile (total memory used given by btop) with a loop of 5 pq.read_table('data.parquet') for pyarrow v19 first, then v18, then v17: Image
    The memory is released after each loop iteration for pyarrow v17 only.

    Because of this issue, experimenting with pyarrow & parquet inside jupyter notebooks leads to increasing memory usage until the next kernel restart. It is less problematic for single task scripts that release the memory at the end anyway.

  3. 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/

  4. timj commented on Jul 14, 2025

    @timj

    Is there any thought that this problem is going to be addressed in a future release or are we going to have to set this envvar for the foreseeable future? We've been pinning our system to an older version because of this but are getting pressure to upgrade.

  5. 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