Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Allocate Memory when using Interrupts #21

Open
alindsay81 opened this issue Sep 4, 2014 · 3 comments
Open

Cannot Allocate Memory when using Interrupts #21

alindsay81 opened this issue Sep 4, 2014 · 3 comments

Comments

@alindsay81
Copy link

So i created a fairly simple python program counting pulses using interrupts and i tested it on the bench with a single input for a couple of days and everything seemed to work well, when i started to use it for real i had 5 inputs and the pulse frequency was a bit higher, after around 22 hours the counting would stop. When i looked into why it could see that Python3 memory use was going up and up everytime a pulse was counted. The first thing i did was to create a test program that only counted and the problem persists with the pi eventually giving the error Cannot Allocate Memory - my test code is here;

def testcount(event):
global Count1
Count1 += 1
print(Count1)

Setup the Digital IO to watch for Rising Edge only

pifacedigital = pifacedigitalio.PiFaceDigital()

listener = pifacedigitalio.InputEventListener(chip=pifacedigital)
listener.register(0, pifacedigitalio.IODIR_RISING_EDGE, testcount)
listener.activate()

Here is a screen shot of the pi back on the bench showing the memory use rising. it only rises when a pulse is received

sc2

@mpruessmeier
Copy link

Hello alindsay81,

i am in a big projekt and had the same problems. the threading of pfacedigitalio is broken in pifacecommon. if you activate the listeners like you do, every event/interrupt is caching memory, this is a "i think known" memory leak.

I tried to find the leak, but could not find it.

this is my workaround:

import pifacedigitalio as pf
import time
import threading

in_tic = [ 0, 0, 0, 0, 0, 0, 0, 0]

Handle holen vom PiFace

pifacedigital = pf.PiFaceDigital()

Tic_in Klasse wird initialisiert

class Tic_in(threading.Thread):

def __init__(self):
    threading.Thread.__init__(self)

def run(self):
    global pifacedigital
    global in_tic
    print("Start on hearing:")

    tic_old = [0,0,0,0,0,0,0,0]
    tic_new = [0,0,0,0,0,0,0,0]
    x=0
    #loop im deamon
    while True:
       tic_new[x]=pifacedigital.input_pins[x].value
       if tic_new[x] and not tic_old[x]:
          in_tic[x] += 1
       tic_old[x] = tic_new[x]
       x+=1
       if x>7: 
          x=0
          time.sleep(0.000001)

#Then start the thread

tic_in = Tic_in()
tic_in.deamon = True
tic_in.start()

this is much faster and the memory leak is gone.
I hope this will work for you.

Greetings from germany, Marc

@alindsay81
Copy link
Author

Hi Marc, Thanks so much for this - With a few small changes it slots perfectly with the rest of my project and works exactly as it should! - CPU use was very high (85%) so i increased the sleep time to 10ms and cpu use came down to 35% - my pulse width is 50ms so 10ms cycle time is no problem for me

@tompreston
Copy link
Member

Hm, I'll flag this for investigation in pifacecommon. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants