Python - Incapaz de liberar la RAM

 
Vista:

Incapaz de liberar la RAM

Publicado por Mikel (1 intervención) el 02/11/2020 17:06:12
Buenas, tengo un programa para leer varias sheets de un excel. Leo los valores, les doy un formato de diccionario para luego usarlo para añadirlos en una base de datos. El problema viene en que no soy capaz de liberar la memoria RAM que se utiliza al leer el excel, y al hacerlo varias veces en un loop se me satura la RAM.
He probado las siguientes cosas:
- Utilizar el garbage collector del modulo gc.
- Obligar a que todas las variables no sean dependientes de ninguna otra (he comprobado que no tengo variables que apuntan a otras, ya que elimino todas menos los input y outputs del script y esta variable no se ve modificada. Los input los copio, no utilizo directamente los input menos variables simples como integers y strings).
- He utilizado las funciones de release_resources() y unload_sheet(theSheetName).

El codigo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def get_excel_trazas(book,theSheetName,myKeyConversor):
    """
    Open and read the Excel file, trazas sheet
    """
    # get the defined worksheet
    defined_sheet = book.sheet_by_name(theSheetName)
 
    # read a the whole sheet row by row
    theWholeExcell=[]
    theModule={}
    num_rows = defined_sheet.nrows
 
    for row_idx in range(1,num_rows):
        # Raed the data of each module. For that:
        # 1) Identify if I am on the same module
        theModuleId=defined_sheet.cell(row_idx, 0)
        if row_idx==1:
            preModule=int(theModuleId.value)
        else:
            if preModule!=int(theModuleId.value):
                # save on the list the dictionary
                theWholeExcell.append(theModule.copy())
                # Erase and declare the theModule dictionary
                theModule.clear()
            else:
                pass
        # 2) Save the value on the dictionary
        theKeyName=defined_sheet.cell(row_idx, 1)
        theValue=defined_sheet.cell(row_idx,2)
        # Save the value in dict
        theModule[theKeyName.value]=theValue.value
        preModule=int(theModuleId.value)
        del theKeyName, theValue, theModuleId
    # Save the last Module (if only one, it saves it)
    theWholeExcell.append(theModule.copy())
    theModule.clear()
    Out=tuple(theWholeExcell)
 
    del defined_sheet, num_rows, row_idx, theWholeExcell, theModule, preModule
    book.unload_sheet(theSheetName)
    book.release_resources()
    gc.collect()
 
    # Return the whole excell dictionary
    return Out

Cualquier ayuda es bienvenida.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder