Skip to content

[C++] Potential memory leak in Parquet reading with Dataset #37630

Closed

Description

@icexelloss

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

Version

Arrow 12.0

Platform

Debian 5.4.228

Description

I have been testing "What is the max rss needed to scan through ~100G of data in a parquet stored in gcs using Arrow C++".

The current answer is about ~6G of memory which seems a bit high so I looked into it. What I observed during the process led me to think that there are some potential cache/memory issues in the dataset/parquet cpp code.

Main observation:
(1) As I am scanning through the dataset, I printed out (a) memory allocated by the memory pool from ScanOptions (b) process rss. I found that while (a) stays pretty stable throughout the scan (stays < 1G), (b) keeps increasing during the scan (looks linear to the number of files scanned).
(2) I tested ScanNode in Arrow as well as an in-house library that implements its own "S3Dataset" similar to Arrow dataset, both showing similar rss usage. (Which led me to think the issue is more likely to be in the parquet cpp code instead of dataset code).
(3) Scan the same dataset twice in the same process doesn't increase the max rss.

Following suggestions from the mailing list, I also did a memory profiling of the test program and the results seem to indicate potential memory leak.

Screenshot from 2023-09-08 09-54-31

Test code

https://gist.github.com/icexelloss/88195de046962e1d043c99d96e1b8b43

Massif output

massif.out.3505660.txt

Component(s)

C++, Parquet

Activity

  1. lidavidm commented on Sep 8, 2023

    @lidavidm
    Member

    You could upload it in a gist and link that here, perhaps?

  2. icexelloss commented on Sep 8, 2023

    @icexelloss
    ContributorAuthor

    You could upload it in a gist and link that here, perhaps?

    I ended up changing the file extension and that worked :)

  3. icexelloss commented on Sep 8, 2023

    @icexelloss
    ContributorAuthor

    Not too familiar with the async/future code (nor massif outputs), but does this look like the Future objects are not destructed somehow (which has a reference to the parquet metadata object?) (You need to expand the massif output in visualizer to see the Future object at the bottom of the call chain)

  4. icexelloss commented on Sep 8, 2023

    @icexelloss
    ContributorAuthor

    cc @mapleFU @wgtmac @pitrou @felipecrv (Folks that have been opining on the mailing list)

  5. mapleFU commented on Sep 8, 2023

    @mapleFU
    Member

    Future in arrow is an shared_future, which means if reference is hold, the object might not be dtor. I guess maybe not that problem, but I'll try to run the script and reproduce the problem later

  6. westonpace commented on Sep 8, 2023

    @westonpace
    Member

    Note that FileFragment in the datasets API caches the parquet metadata (with no option to disable this at the moment). So if you are scanning many files you will see memory grow over the lifetime of the scan as more and more metadatas are cached. I would expect a second scan would not grow the memory.

  7. icexelloss commented on Sep 8, 2023

    @icexelloss
    ContributorAuthor

    Note that FileFragment in the datasets API caches the parquet metadata (with no option to disable this at the moment). So if you are scanning many files you will see memory grow over the lifetime of the scan as more and more metadatas are cached. I would expect a second scan would not grow the memory.

    Thanks @westonpace, can you give a pointer to where that is happening?

  8. westonpace commented on Sep 8, 2023

    @westonpace
    Member

    In file_parquet.h. The ParquetFileFragment class has these variables:

      /// Indices of row groups selected by this fragment,
      /// or std::nullopt if all row groups are selected.
      std::optional<std::vector<int>> row_groups_;
    
      std::vector<compute::Expression> statistics_expressions_;
      std::vector<bool> statistics_expressions_complete_;
      std::shared_ptr<parquet::FileMetaData> metadata_;
      std::shared_ptr<parquet::arrow::SchemaManifest> manifest_;
    

    They initially start empty/null and are initialized during a call to EnsureCompleteMetadata.

  9. icexelloss commented on Sep 8, 2023

    @icexelloss
    ContributorAuthor

    In file_parquet.h. The ParquetFileFragment class has these variables:

      /// Indices of row groups selected by this fragment,
      /// or std::nullopt if all row groups are selected.
      std::optional<std::vector<int>> row_groups_;
    
      std::vector<compute::Expression> statistics_expressions_;
      std::vector<bool> statistics_expressions_complete_;
      std::shared_ptr<parquet::FileMetaData> metadata_;
      std::shared_ptr<parquet::arrow::SchemaManifest> manifest_;
    

    They initially start empty/null and are initialized during a call to EnsureCompleteMetadata.

    Hmm, what do you mean by they are cached? Doesn't ParquetFileFragment get dtored after we scanned the fragment?

  10. icexelloss commented on Sep 8, 2023

    @icexelloss
    ContributorAuthor

    Oh I see we keep a vector of Fragment, each Fragment has a shared_ptr to the schema of the file

  11. westonpace commented on Sep 8, 2023

    @westonpace
    Member

    Yes, the fragments are part of the dataset. So they stay around until the dataset is destroyed which has to be after the scan.

  12. icexelloss commented on Sep 11, 2023

    @icexelloss
    ContributorAuthor

    In a local build I cleared

      std::shared_ptr<parquet::FileMetaData> metadata_;
      std::shared_ptr<parquet::arrow::SchemaManifest> manifest_;
    

    after scanning a fragment and things appear to be in better shape. Memory usage goes from 4G -> 700+Mb.

    There still seem to be some leaking that I couldn't figure out but at least things seem to be in a much better shape after clearing those two fields.
    massif-after-fix
    massif.out.593871.txt

  13. 11 remaining items

  14. pitrou commented on Jan 22, 2025

    @pitrou
    Member

    I've submitted a draft PR that adds a cache_metadata option when scanning.

    Here are some memory consumption measurements on a synthetic dataset similar to the one in #45287:

    • with cache_metadata=True (the default):
    $ /usr/bin/time -f "\n%E real\n%U user\n%S sys\n%M kB peak RSS" python  -c 'import pyarrow.dataset as pd; pd.dataset("/home/antoine/arrow/data/bamboo-streaming-parquet-test-data/1000col-dataset/").to_table()'
    
    0:11.01 real
    107.66 user
    4.37 sys
    2069284 kB peak RSS
    • with cache_metadata=False:
    $ /usr/bin/time -f "\n%E real\n%U user\n%S sys\n%M kB peak RSS" python  -c 'import pyarrow.dataset as pd; pd.dataset("/home/antoine/arrow/data/bamboo-streaming-parquet-test-data/1000col-dataset/").to_table(cache_metadata=False)'
    
    0:10.97 real
    109.11 user
    3.87 sys
    1381696 kB peak RSS
  15. pitrou commented on Jan 22, 2025

    @pitrou
    Member

    @icexelloss It would be useful it you could test the above PR on a more real-world dataset.

  16. icexelloss commented on Jan 22, 2025

    @icexelloss
    ContributorAuthor

    Thanks @pitrou! We actually already have an internal patch similar to what you have now but still observed higher than expected memory usage when scanning a parquet dataset - my colleague @timothydijamco is working a repro and should have it soon

    I also noticed that you also cleared original_metadata_.reset(); - we will test that as well

  17. added this to the 20.0.0 milestone on Feb 11, 2025
  18. pitrou commented on Feb 11, 2025

    @pitrou
    Member

    Issue resolved by pull request 45330
    #45330

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions