How do I make an infinite loop without bursting the memory?

Asked

Viewed 4,783 times

5

I’m trying to make a program that requests on a DDE server, so I need to collect the data every 0.1 sec. But with each iteration of the program the computer memory increases and at the end for the script.

I’m trying like this:

def requsitaDDE (y, j):    
    global quote

    if j == 4:
        time.sleep(.1)
        #old_quote = quote[0]
        quote = None
        quote = []
        return requsitaDDE(y, 0)

     else:
        current_quote = QUOTE_client.request(y[j])
        quote.append(current_quote)

        requsitaDDE(y, j + 1)

    requsitaDDE(y,0)

But I’ve done it too:

while 1:
    time.sleep(.1)

    quote = []     

    for i in symbols:
        current_quote = QUOTE_client.request(i)
        quote.insert(y, current_quote)
        y += 1
    print quote

    y = 0

Expensive,

The memory increment problem continues, now I did as @Marcos suggested:

while 1:
gc.collect()
#time.sleep(.1)
mem_usage = memory_usage(-1, interval=.2, timeout=1)
print(mem_usage)
y+= 1
print y

#if y == 20000:
#    break

cur.execute("SELECT MAX(rowid) FROM winj") # use your col
linhaBd = cur.fetchone()
linhaBd[0]

if old_quote != QUOTE_client.request(symbols[0]):

    for i in symbols:

        current_quote = QUOTE_client.request(i)#.split("\t") 

        if symbols.index(i) == 0:
            cur.execute('INSERT INTO winj(rowid, preco) VALUES(?, ?)' , [(linhaBd[0] + 1), current_quote])
            conn.commit()  
            old_quote = current_quote
            print current_quote

        if symbols.index(i) == 1:
            cur.execute('UPDATE winj SET quantidade = ? WHERE rowid = ?' , [current_quote, (linhaBd[0] + 1)])
            conn.commit()

        if symbols.index(i) == 2:
            cur.execute('UPDATE winj SET hora = ? WHERE rowid = ?' , [current_quote, (linhaBd[0] + 1)])
            conn.commit()

        if symbols.index(i) == 3:
            cur.execute('UPDATE winj SET data = ? WHERE rowid = ?' , [current_quote, (linhaBd[0] + 1)])
            conn.commit()     

If anyone has any ideas, I’d appreciate it.

  • Have tried calling Garbage Collector { gc.Collect() } at the end of each iteration ?

  • I added the following line: collected = gc.Collect() print "Garbage Collector: collected %d Objects." % (collected) responds that it is with 0 objects. is now showing this error: Maximum recursion Depth exceeded in instancecheck

  • Use the iterative version of the algorithm ... your program is making infinite recursive calls

  • Felipe, I returned to the recursive version, but the memory continues to increase. Would have something to do in relation to this?

1 answer

5


Memory is increasing because every iteration is adding a value to the variable quote.

One solution is instead of Ravas this data collected in memory is to save to disk. It may be in some file or in a database.

Example with writing to files:

while 1:
    time.sleep(.1)

    for i in symbols:
        current_quote = QUOTE_client.request(i)

        arq = open('/tmp/lista.txt', 'a')
        texto = y + current_quote
        arq.write(texto)
        arq.close()
        y += 1

    y = 0

When you need to consult the collected data, just access the file.

  • 1

    hi Mark - If you open the file with "w" mode, you will always reset the file and write everything again. The correct thing to do is to open the file with "a" - But the best thing to do is to open the file outside of "for", then open it once. (I also recommend checking the "with" command to open and close files)

  • Marcos, I did the same iteration using getsizeof(quote), but it doesn’t increase. Also, I made the variable receive [], then NONE, and "del". In all of them it continues to increase. In this reading I do in while, I take 4 values and add in a sqlite for later calculation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.