You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C++] Potential memory leak in Parquet reading with Dataset #37630
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.
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)
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
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.
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?
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.
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?
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.out.593871.txt
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
Activity
lidavidm commentedon Sep 8, 2023
You could upload it in a gist and link that here, perhaps?
icexelloss commentedon Sep 8, 2023
I ended up changing the file extension and that worked :)
icexelloss commentedon Sep 8, 2023
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)
icexelloss commentedon Sep 8, 2023
cc @mapleFU @wgtmac @pitrou @felipecrv (Folks that have been opining on the mailing list)
mapleFU commentedon Sep 8, 2023
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
westonpace commentedon Sep 8, 2023
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.
icexelloss commentedon Sep 8, 2023
Thanks @westonpace, can you give a pointer to where that is happening?
westonpace commentedon Sep 8, 2023
In
file_parquet.h. TheParquetFileFragmentclass has these variables:They initially start empty/null and are initialized during a call to
EnsureCompleteMetadata.icexelloss commentedon Sep 8, 2023
Hmm, what do you mean by they are cached? Doesn't ParquetFileFragment get dtored after we scanned the fragment?
icexelloss commentedon Sep 8, 2023
Oh I see we keep a vector of Fragment, each Fragment has a shared_ptr to the schema of the file
westonpace commentedon Sep 8, 2023
Yes, the fragments are part of the dataset. So they stay around until the dataset is destroyed which has to be after the scan.
icexelloss commentedon Sep 11, 2023
In a local build I cleared
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.out.593871.txt
11 remaining items
pitrou commentedon Jan 22, 2025
I've submitted a draft PR that adds a
cache_metadataoption when scanning.Here are some memory consumption measurements on a synthetic dataset similar to the one in #45287:
cache_metadata=True(the default):cache_metadata=False:pitrou commentedon Jan 22, 2025
@icexelloss It would be useful it you could test the above PR on a more real-world dataset.
icexelloss commentedon Jan 22, 2025
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 wellpitrou commentedon Feb 11, 2025
Issue resolved by pull request 45330
#45330