BUG: Memory leak when setting Series value via setitem · Issue #47172 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd import gc import os import psutil

create example data

items = [f'item_{i}' for i in range(10_000)] data = {} for i, col in enumerate(items[:1000]): data[col] = [1]*len(items) df = pd.DataFrame(index=items, data=data) gc.collect()

process = psutil.Process(os.getpid()) rss = process.memory_info().rss/1024/1024

for item in df.columns[:10]: df[item][item] = -10 gc.collect() new_rss = process.memory_info().rss/1024/1024 print('{:.2f} MiB'.format(new_rss-rss))

Issue Description

In pandas version 1.4.2 the memory usage increases at each iteration of the last for loop, outputs:

76.55 MiB
152.99 MiB
229.16 MiB
305.08 MiB
381.00 MiB
456.92 MiB
532.58 MiB
608.24 MiB
683.96 MiB
759.51 MiB

Expected Behavior

In pandas version 1.3.5 the memory usage remains constant, output:

0.00 MiB
0.00 MiB
0.00 MiB
0.00 MiB
0.00 MiB
0.00 MiB
0.00 MiB
0.00 MiB
0.00 MiB
0.00 MiB

Installed Versions

INSTALLED VERSIONS
------------------
commit           : 4bfe3d07b4858144c219b9346329027024102ab6
python           : 3.8.12.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.10.0-13-amd64
Version          : #1 SMP Debian 5.10.106-1 (2022-03-17)
machine          : x86_64
processor        :
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.4.2
numpy            : 1.21.6
pytz             : 2022.1
dateutil         : 2.8.2
pip              : 22.1.1
setuptools       : 56.0.0
Cython           : 0.29.30
pytest           : None
hypothesis       : None
sphinx           : 4.5.0
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : 1.0.2
psycopg2         : None
jinja2           : 3.1.2
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
brotli           : None
fastparquet      : None
fsspec           : None
gcsfs            : None
markupsafe       : 2.1.1
matplotlib       : 3.5.1
numba            : 0.55.1
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyreadstat       : None
pyxlsb           : None
s3fs             : None
scipy            : 1.8.0
snappy           :
sqlalchemy       : 1.4.36
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
zstandard        : None