Raspberry Pi tricks and tips

Memory & CPU

Disable services you probably don't need, wpa_supplicant, triggerhappy and avahi

Especially on the older models with limited amount of memory available, disabling unnecessary services can free up a bit of that precious memory. For example if you don't have wireless hardware installed, running wpa_supplicant is a waste of resources. Also avahi is quite useless and so is the triggerhappy hotkey daemon (especially if you are running headless!).

Stop the services with:

systemctl stop wpa_supplicant avahi-daemon triggerhappy

And disable them so they don't start up

systemctl disablep wpa_supplicant avahi-daemon triggerhappy

Adjust CPU/GPU memory split

If you are using your system without X11 or don't have a camera plugged in, the memory set aside for the GPU is pretty much all just wasted. On the low memory models this can make a world of difference as the default is to set aside 64MB for the GPU. And on the low memory models (256MB/512MB) wasting 64MB is quite a lot. You can adjust the memory set aside for the GPU in /boot/config.txt, the values can be 16/32/64/128/256

For a CLI console system without GUI/X11 and no camera, set

gpu_mem=16

Unfortunately, even if running otherwise headless, with camera the GPU needs at least 64MB of memory for still image capture and 128MB for video capture, so adjust accordingly. So if you know that you only make still capture you can save 64MB!

gpu_mem=64

The memory split is setup at boot so remember to reboot after changing the settting. You can verify the amount of memory used for the GPU by running vcgencmd:

vcgencmd get_mem gpu

Storage

Use external USB 3.0 storage (On Pi 4)

SD cards are quite slow and fragile, highly recommended to use external USB 3.0 storage for anything requring substantial amout of I/O, like compilation or any kind of databases. With cheap USB 3.0 to SATA adapters you can utilise SSD or HDDs with your Pi. For example I was donated a bunch of old 1TB SATA drives that are perfect for the Pi.

hdparm -t read speed test
Storage Media Model Speed
SD card SA16G 20.90 MB/sec
HDD Barracuda ES.2 136.79 MB/sec
SSD   TBA

Audio and Video media

Hardware h264 video capture with gstreamer

Sure, you can always use raspivid to capture h264 video, but with gstreamer it is possible to save in various other container formats or for example stream over the network, depending on your needs.

Capture h264 Full HD, 1920x1080 video save in matroska container:

gst-launch-1.0 -e v4l2src ! video/x-h264,framerate=30/1,width=1920,height=1080 ! queue ! h264parse ! matroskamux ! filesink location=capture.mkv

Use your Raspberry Pi as video input to OBS

First, install GStreamer OBS pluging on your streaming computer.

Add a "GStreamer source" to your Scene with the following pipeline:

udpsrc ! application/x-rtp,payload=127 ! rtph264depay ! decodebin ! video/x-raw,width=1920,height=1080 ! videoscale ! video.

On the Raspberry Pi start the h264 streaming pipeline:

gst-launch-1.0 -e v4l2src ! video/x-h264,framerate=30/1,width=1920,height=1080 ! queue ! h264parse config-interval=1 ! rtph264pay ! queue ! udpsink host=IP-OF-YOUR-OBS-HOST

You can even stream and make a backup recording at the same time on your Pi. In that case try this pipeline:

gst-launch-1.0 -e v4l2src ! video/x-h264,framerate=25/1,width=1920,height=1080 ! queue ! h264parse config-interval=1 ! tee name=t ! queue ! rtph264pay ! udpsink host=IP-OF-YOUR-OBS-HOST
 t. ! queue ! matroskamux ! filesink location=backupcopy.mkv

Do you have any good tips and tricks ? Feedback ? Please comment below.