You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "RizzoV (via GitHub)" <gi...@apache.org> on 2023/10/03 13:14:15 UTC
[I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
RizzoV opened a new issue, #37989:
URL: https://github.com/apache/arrow/issues/37989
### Describe the bug, including details regarding any error messages, version, and platform.
### Issue Description
(continuing from https://github.com/pandas-dev/pandas/issues/55296)
`pyarrow.Table.from_pandas()` causes a memory leak on DataFrames containing nested structs. A sample problematic data schema and a compliant data generator is included in the Reproducible Example below.
From the Reproducible Example:
- 1st `pd.DataFrame.to_parquet()` call:
```
Line # Mem usage Increment Occurrences Line Contents
=============================================================
74 91.9 MiB 91.9 MiB 1 @profile
75 def convert_df_to_table(df: pd.DataFrame):
76 91.9 MiB 0.0 MiB 1 table = pa.Table.from_pandas(df, schema=pa.schema(sample_schema))
```
- 2000th call:
```
Line # Mem usage Increment Occurrences Line Contents
=============================================================
74 140.1 MiB 140.1 MiB 1 @profile
75 def convert_df_to_table(df: pd.DataFrame):
76 140.1 MiB 0.0 MiB 1 table = pa.Table.from_pandas(df, schema=pa.schema(sample_schema))
```
- 10000th call:
```
Line # Mem usage Increment Occurrences Line Contents
=============================================================
74 329.4 MiB 329.4 MiB 1 @profile
75 def convert_df_to_table(df: pd.DataFrame):
76 329.5 MiB 0.0 MiB 1 table = pa.Table.from_pandas(df, schema=pa.schema(sample_schema))
```
### Reproducible Example
```python
import os
import string
import sys
from random import choice, randint
from uuid import uuid4
import pandas as pd
import pyarrow as pa
from memory_profiler import profile
sample_schema = pa.struct(
[
("a", pa.string()),
(
"b",
pa.struct(
[
("ba", pa.list_(pa.string())),
("bc", pa.string()),
("bd", pa.string()),
("be", pa.list_(pa.string())),
(
"bf",
pa.list_(
pa.struct(
[
(
"bfa",
pa.struct(
[
("bfaa", pa.string()),
("bfab", pa.string()),
("bfac", pa.string()),
("bfad", pa.float64()),
("bfae", pa.string()),
]
),
)
]
)
),
),
]
),
),
("c", pa.int64()),
("d", pa.int64()),
("e", pa.string()),
(
"f",
pa.struct(
[
("fa", pa.string()),
("fb", pa.string()),
("fc", pa.string()),
("fd", pa.string()),
("fe", pa.string()),
("ff", pa.string()),
("fg", pa.string()),
]
),
),
("g", pa.int64()),
]
)
def generate_random_string(str_length: int) -> str:
return "".join(
[choice(string.ascii_lowercase + string.digits) for n in range(str_length)]
)
@profile
def convert_df_to_table(df: pd.DataFrame) -> None:
table = pa.Table.from_pandas(df, schema=pa.schema(sample_schema))
def generate_random_data():
return {
"a": [generate_random_string(128)],
"b": [
{
"ba": [generate_random_string(128) for i in range(50)],
"bc": generate_random_string(128),
"bd": generate_random_string(128),
"be": [generate_random_string(128) for i in range(50)],
"bf": [
{
"bfa": {
"bfaa": generate_random_string(128),
"bfab": generate_random_string(128),
"bfac": generate_random_string(128),
"bfad": randint(0, 2**32),
"bfae": generate_random_string(128),
}
}
],
}
],
"c": [randint(0, 2**32)],
"d": [randint(0, 2**32)],
"e": [generate_random_string(128)],
"f": [
{
"fa": generate_random_string(128),
"fb": generate_random_string(128),
"fc": generate_random_string(128),
"fd": generate_random_string(128),
"fe": generate_random_string(128),
"ff": generate_random_string(128),
"fg": generate_random_string(128),
}
],
"g": [randint(0, 2**32)],
}
def main():
for i in range(10000):
df = pd.DataFrame.from_dict(generate_random_data())
# pa.jemalloc_set_decay_ms(0)
convert_df_to_table # memory leak
if __name__ == "__main__":
main()
```
### Installed Versions
<details>
```
INSTALLED VERSIONS
------------------
python : 3.10.9.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Fri Sep 15 13:39:52 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : it_IT.UTF-8
LOCALE : it_IT.UTF-8
pyarrow : 13.0.0
pandas : 2.1.1
numpy : 1.26.0
```
</details>
### Component(s)
Python
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "Ashokcs94 (via GitHub)" <gi...@apache.org>.
Ashokcs94 commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-1842060348
@RizzoV / @jorisvandenbossche : any solution for the memory leak in `to_parquet()` ?, we are also facing this issue for long time
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "nicksilver (via GitHub)" <gi...@apache.org>.
nicksilver commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-2454572246
Not sure if I should open up a new bug report, but I am having the same memory leak issue when writing a list of strings (not dicts) to parquet from pandas. It seems like a similar issue and probably similar fix.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "amoeba (via GitHub)" <gi...@apache.org>.
amoeba commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-2483547807
Hi @nicksilver or @DatSplit, a new issue would be good. What would be most helpful in that issue would be a minimal reproducer like the one in https://github.com/apache/arrow/issues/37989#issuecomment-1745488061.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "DatSplit (via GitHub)" <gi...@apache.org>.
DatSplit commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-2455023495
@nicksilver I suspect that I'm having the same issue right now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "nicksilver (via GitHub)" <gi...@apache.org>.
nicksilver commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-2543903113
Hi @kr-hansen and @amoeba, I did not open a new issue although I believe the leak still exists. My workaround was to convert my list of strings to a list of dictionaries to comply with this fix. When I did this the memory leak was gone. I believe if you used the same minimal reproducer from this issue but saved a list of strings, instead of dicts, you would get the leak. I'm probably not the best person to open a new issue -- I am not much of a developer -- this was just something I stumbled upon while trying to reformat some json files.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "kr-hansen (via GitHub)" <gi...@apache.org>.
kr-hansen commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-2537377399
@nicksilver or @DatSplit did either of you open a new issue? I see #44295 as a minimal reproducer case, but that was before your posts here. I'm also hitting some type of memory leak related to this. Do we need another open issue, or is that one from October sufficient to dig in on?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "kr-hansen (via GitHub)" <gi...@apache.org>.
kr-hansen commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-2604517486
Hmmm what version were you using @DatSplit? When working with very large data (Dataframes ~200 GB), I continue to see memory crashes with `pyarrow` that I don't get with `fastparquet`. While when writing out `fastparquet` keeps my memory flat during the writing out, `pyarrow` has a huge spike that 3-5.5x the memory footprint of the actual data itself.
This is for `pyarrow 19.0.0` for me.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "RizzoV (via GitHub)" <gi...@apache.org>.
RizzoV commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-1842374759
@Ashokcs94 no solution from my side sadly, we still have to work around it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-1745488061
@RizzoV thanks for the report and nice reproducer!
I can reproduce this running your example with memray:

From the memray stats, it looks like the memory being held at the end is mostly coming from the list with strings, so somehow the conversion to arrow seems to keep those list object alive (haven't yet looked at how that is possible, though).
And also the pandas metadata conversion (the json dump) seems to accumulate memory, although that's a bit strange (but I don't see that in the smaller reproducer below).
It seems it is specifically happens when having a list that is nested inside another column (eg struct of list), so I can reproduce the observation as well with this simplified example:
```python
import string
from random import choice
import pandas as pd
import pyarrow as pa
sample_schema = pa.struct(
[
( "a", pa.struct([("aa", pa.list_(pa.string()))])),
]
)
def generate_random_string(str_length: int) -> str:
return "".join(
[choice(string.ascii_lowercase + string.digits) for n in range(str_length)]
)
def generate_random_data():
return {
"a": [{"aa": [generate_random_string(128) for i in range(50)]}],
}
def main():
for i in range(10000):
df = pd.DataFrame.from_dict(generate_random_data())
# pa.jemalloc_set_decay_ms(0)
table = pa.Table.from_pandas(df, schema=pa.schema(sample_schema))
if __name__ == "__main__":
main()
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-1999919031
Issue resolved by pull request 40412
https://github.com/apache/arrow/pull/40412
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "chunyang (via GitHub)" <gi...@apache.org>.
chunyang commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-1984770427
I believe I found a fix for this in https://github.com/apache/arrow/pull/40412, please take a look :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche closed issue #37989: [Python] `pyarrow.Table.from_pandas()` causing memory leak
URL: https://github.com/apache/arrow/issues/37989
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pyarrow.Table.from_pandas()` causing memory leak [arrow]
Posted by "DatSplit (via GitHub)" <gi...@apache.org>.
DatSplit commented on issue #37989:
URL: https://github.com/apache/arrow/issues/37989#issuecomment-2582975981
Hi @amoeba @kr-hansen @nicksilver, after reproducing original example mentioned of #37989 I believe this specific issue is fixed:
1st pa.Table.from_pandas() call:
`Line # Mem usage Increment Occurrences Line Contents
=============================================================
74 81.0 MiB 81.0 MiB 1 @profile
75 def convert_df_to_table(df: pd.DataFrame) -> None:
76 81.0 MiB 0.0 MiB 1 table = pa.Table.from_pandas(df, schema=pa.schema(sample_schema))`
10000th call:
`Line # Mem usage Increment Occurrences Line Contents
=============================================================
74 81.0 MiB 81.0 MiB 1 @profile
75 def convert_df_to_table(df: pd.DataFrame) -> None:
76 81.0 MiB 0.0 MiB 1 table = pa.Table.from_pandas(df, schema=pa.schema(sample_schema))`
So, quite frankly I do not know what might have caused our issues.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org