用Pandas创建缓冲区时内存泄漏?

2024-10-04 03:24:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我用pandas做了一个环形缓冲区,但是内存的使用一直在增长。我做错什么了?在

以下是代码(从问题的第一篇文章开始编辑):

import pandas as pd
import numpy as np
import resource


tempdata = np.zeros((10000,3))
tdf = pd.DataFrame(data=tempdata, columns = ['a', 'b', 'c'])

i = 0
while True:
    i += 1
    littledf = pd.DataFrame(np.random.rand(1000, 3), columns = ['a', 'b', 'c'])
    tdf = pd.concat([tdf[1000:], littledf], ignore_index = True)
    del littledf
    currentmemory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    if i% 1000 == 0:
        print 'total memory:%d kb' % (int(currentmemory)/1000)

我得到的是:

^{pr2}$

不确定是否与此有关:

https://github.com/pydata/pandas/issues/2659

在MacBook Air上测试了Python


Tags: columnsimporttruedataframepandasasnpresource
1条回答
网友
1楼 · 发布于 2024-10-04 03:24:21

与其使用concat,为什么不就地更新数据帧i % 10将确定您向每个更新写入的1000行槽。在

i = 0
while True:
    i += 1
    tdf.iloc[1000*(i % 10):1000+1000*(i % 10)] = np.random.rand(1000, 3)
    currentmemory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    if i% 1000 == 0:
        print 'total memory:%d kb' % (int(currentmemory)/1000)

相关问题 更多 >