User avatar
KarlS
Posts: 42
Joined: Fri Oct 12, 2012 1:10 pm

memory leak?

Fri Aug 14, 2015 2:34 pm

I'm using the camera board as a weather cam. A cronjob runs a script every 15 minutes to take a picture and upload it to WeatherUnderground. This job has been running for months without any problems. Two days ago I noticed that increasingly larger parts at the bottom of the pictures were "grayed out".

Image Image Image

A reboot solved the problem. I use raspistill to take the picture, imagemagick to do some post-processing (crop / resize / annotate) and ftp to upload the pictures. Could a memory leak be the problem?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6891
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: memory leak?

Fri Aug 14, 2015 2:44 pm

Possible, but probably unlikely.
Grey is typically the default if the JPEG data is truncated. What synchronisation have you got between raspistill and imagemagik to ensure imagemagik doesn't start processing the image before raspistill has finished writing it? Or could the storage that raspistill is saving to have become full?
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
KarlS
Posts: 42
Joined: Fri Oct 12, 2012 1:10 pm

Re: memory leak?

Fri Aug 14, 2015 2:59 pm

I'm using a 20MB ram disk to store the pictures. The ram is cleared at the beginning of the script:

Code: Select all

#!/bin/bash

picString=`date +"%H:%M on %d %B %Y"`
sudo rm /mnt/ram/*.jpg

#take photo
sudo raspistill -n -st -sh 20 $1 -mm matrix -o /mnt/ram/photo.jpg

#resize and annotate
sudo convert /mnt/ram/photo.jpg \
	-resize 800x600 \
	-sigmoidal-contrast 3,0% \
	-font Helvetica-Bold -pointsize 25 \
	-fill white \
	-annotate +510+585 "$picString" \
	-define jpeg:extent=60KB /mnt/ram/image.jpg

#upload images to Wunderground
HOST=webcam.wunderground.com
USER=***********
PASS=***********
ftp -pinv $HOST << EOF
user $USER $PASS
binary
lcd /mnt/ram
put image.jpg
bye
EOF
I monitored the Use% of the ram disk and it never went over 12%

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6891
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: memory leak?

Fri Aug 14, 2015 3:15 pm

I suspected you were going to say that you were using a RAM disk :)
I don't know why or how, but I would say that saving to that RAM disk was failing and truncating the file. It explains all the symptoms, and why a reboot solved it.
Could imagemagik be dropping a temp file there that wasn't always cleaned up? You were only deleting .jpg files, so if anything else managed to get on there it wouldn't be removed.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
jbeale
Posts: 3438
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: memory leak?

Fri Aug 14, 2015 3:16 pm

I had a similar situation some time ago with raspimjpeg and the cause was a timing synchronization problem between the creator of the JPEG image and the consumer of it. It is interesting that the problem you see has happened only after some months. I can only speculate, even if the actual JPEG is stored in a ramdisk, that there may be some other process with timing affected by a gradually slower SD card memory due to filesystem fragmentation or something like that.

Although, with the same script that runs raspistill and then imagemagick, that seems to rule out the possibility of a timing issue, if raspistill returns only after the file write is complete... but is that always true? Or is there some internal pipelining so that possibly, the buffer is still being written out for some short time after the raspistill process returns?

User avatar
KarlS
Posts: 42
Joined: Fri Oct 12, 2012 1:10 pm

Re: memory leak?

Fri Aug 14, 2015 3:25 pm

Thanks 6by9 and jbeale

I changed the script to delete everything in the ram disk (not just jpg's) and put a new sd card into the B+. Hopefully that solves the problem ...

User avatar
jbeale
Posts: 3438
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: memory leak?

Fri Aug 14, 2015 6:06 pm

It feels like a band-aid without understanding the root cause, but if this is not a time critical application overall, I wonder if it would be useful to put a fixed time delay of a few seconds in your script, in between "sudo raspistill..." and "sudo convert..."

User avatar
KarlS
Posts: 42
Joined: Fri Oct 12, 2012 1:10 pm

Re: memory leak?

Fri Aug 14, 2015 6:47 pm

Sure, I can do that. However, now I have changed 3 possible sources for the error, and I'm still not sure what caused it (although I tend to agree with 6by9 and suspect the ram disk). This application runs unattended at my cabin and it can be weeks before I can check on it again ...

Return to “Camera board”