简体   繁体   中英

spyder does not free memory after running code

I am running a python code in Spyder. When i start the code memory is on 22%, but after finishing the code the memory remain on 64%. I must restart kernel or end process from task manager to back to 22%. Why this situation occurs? and how to handle it?

I attached the my code for more details.

import csv
import re
import openpyxl
import os
papers_dir = os.listdir('Persian - Copy')


#Journals
for y in papers_dir:
    papers = []
    if y.endswith('.xlsx'):
        wb = openpyxl.load_workbook('Persian - Copy/'+y)
        sheet = wb.active
        for z in range(2, sheet.max_row+1):
            id_ = str(sheet.cell(row=z, column=1).value)
            field = str(sheet.cell(row=z, column=2).value).replace('ي','ی').replace('ك','ک').split()
            field1 = str(sheet.cell(row=z, column=4).value).replace('ي','ی').replace('ك','ک').replace('None', '').split('،')
            field2 = re.sub(re.compile('<.*?>'), '', str(sheet.cell(row=z, column=3).value))
            field2 = field2.replace('ي','ی').replace('ك','ک').replace('لطفا برای مشاهده چکیده به متن کامل (PDF) مراجعه فرمایید.','').replace(' لطفا برای مشاهده چکیده به متن کامل (PDF) مراجعه فرمایید.','').replace('None', '').replace('.',' ').split()
            f = [id_, (field + field1 + field2)]
            papers.append(f)
        wb.close()
        sheet = 0
        print(y)

    #Making Unique Rows
    uniques = []

    x_0 = []
    for x in papers:
        if x[0] not in x_0:
            x_0.append(x[0])
            uniques.append(x)

    with open('all.csv', 'a+', newline='', encoding='UTF-16') as un:
        writer = csv.writer(un, delimiter= '\t')
        for x in uniques:
            writer.writerow(x)
    papers = 0
    uniques = 0
    x_0 = 0
    f = 0

To combat this I used pickle to save my objects and then I restarted the console every time.

It is pedantic but it helps combat the memory usage. The developers are working on it I think but this issue has been around for a year or so.

关闭运行代码的ipython控制台应释放它使用的内存。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM