I've written a python script which can make video's using a Hawkeye camera. While running this script, I feel my Pi gets slower and slower. Besides that, my free RAM shown by the tool 'free' keeps decreasing. Could it be I made some kind of memory leak?
The code i'm using:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import hashlib
import signal
import RPi.GPIO as GPIO
import os.path
from os import path
import glob
from shutil import copyfile
REC=20
SWITCH=21
VideoDuration=30
def TestScript():
GPIO.output(SWITCH, 1)
while(not path.exists('/media/pi/SDHC')):
pass
time.sleep(2)
GPIO.output(SWITCH, 1)
LatestVideo = get_latest_file()
copyfile("/media/pi/SDHC/Hawkeye/Movie/" + LatestVideo, "/home/pi/Videos/" + LatestVideo)
while(not path.exists("/home/pi/Videos/" + LatestVideo)):
pass
os.remove("/media/pi/SDHC/Hawkeye/Movie/" + LatestVideo)
time.sleep(2)
os.system("sudo umount /media/pi/SDHC")
time.sleep(2)
GPIO.output(SWITCH, 0)
time.sleep(3)
GPIO.output(SWITCH, 1)
time.sleep(2)
GPIO.output(SWITCH, 0)
time.sleep(3)
def get_latest_file():
fullpath = "/media/pi/SDHC/Hawkeye/Movie/*"
list_of_files = glob.glob(fullpath)
if not list_of_files:
return None
latest_file = max(list_of_files, key=os.path.getctime)
_, filename = os.path.split(latest_file)
return filename
def RecordVideo(Duration):
GPIO.output(REC, 0)
time.sleep(2)
GPIO.output(REC, 1)
time.sleep(Duration)
GPIO.output(REC, 0)
time.sleep(2)
GPIO.output(REC, 1)
###### Execute test script ######
cnt = 0
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(REC, GPIO.OUT)
GPIO.setup(SWITCH, GPIO.OUT)
while (cnt < 1000):
## Configure GPIO ##
START = time.time()
RecordVideo(VideoDuration-1)
TestScript()
cnt = cnt +1
END = time.time()
print("Total duration: " + str(END-START))
Could it be that I start a process everytime, but never close it? Could someone please help me out here?
Edit: The output from top can be found below
Here is shown that the Pi is running out of free RAM and the cache/buffer is filling with RAM. However I feel the Pi is getting slower and slower. Eventually I get a error in my python script that it's unable to concatenate str and NoneType in the copyfile function. I suspect this happens because the Pi ran out of RAM and can't save the latest filename to the variable LatestVideo anymore.
Thanks in advance