Pandas: gdzie jest wyciek pamięci?
Stoję przed problemem wycieków pamięci za pomocąpandas
biblioteka wpyton. tworzępandas.dataframe
obiekty w mojej klasie i mam metodę, która zmienia rozmiar ramki danych zgodnie z moimi warunkami. Po zmianie rozmiaru ramki danych i utworzeniu nowego obiektu pand przepisuję oryginalną ramkę pandas.dataframe w mojej klasie. Ale użycie pamięci jest bardzo wysokie, nawet po znacznym zmniejszeniu początkowej tabeli. Jakiś kod na krótki przykład (nie napisałem menedżera procesów, zobacz menedżera zadań):
import time, string, pandas, numpy, gc
class temp_class ():
def __init__(self, nrow = 1000000, ncol = 4, timetest = 5):
self.nrow = nrow
self.ncol = ncol
self.timetest = timetest
def createDataFrame(self):
print('Check memory before dataframe creating')
time.sleep(self.timetest)
self.df = pandas.DataFrame(numpy.random.randn(self.nrow, self.ncol),
index = numpy.random.randn(self.nrow), columns = list(string.letters[0:self.ncol]))
print('Check memory after dataFrame creating')
time.sleep(self.timetest)
def changeSize(self, from_ = 0, to_ = 100):
df_new = self.df[from_:to_].copy()
print('Check memory after changing size')
time.sleep(self.timetest)
print('Check memory after deleting initial pandas object')
del self.df
time.sleep(self.timetest)
print('Check memory after deleting copy of reduced pandas object')
del df_new
gc.collect()
time.sleep(self.timetest)
if __name__== '__main__':
a = temp_class()
a.createDataFrame()
a.changeSize()
Przed utworzeniem ramki danych mam około. 15 MB pamięci
Po utworzeniu - 67 MB
Po zmianie rozmiaru - 67 mb
Po usunięciu oryginalnej ramki danych - 35 MB
Po usunięciu zmniejszonej tabeli - 31 mb.
16 MB?
Używam Pythona 2.7.2 (x32) na komputerze z systemem Windows 7 (x64), pandami.wersja wynosi 0.7.3. numpy.wersja to 1.6.1