Testing A Raspberry Pi

It can be a good idea to test a Raspberry Pi before committing it to a project or you may wish to test it for problems if it has started freezing.
Note: a common problem that gives the appearance that Linux has completely frozen is running out of RAM. Many Raspberry Pis do not have a lot of memory and you may wish to investigate if this is the problem first before looking into if the Pi is broken.
Testing the Memory
A memory tester running as a regular user program won't be able to allocate all of the memory on a Raspberry Pi, so you should aim to test slightly less than the amount installed. At about 80% of memory usage the operating system will usually try and kill the memory tester process. You can probably test 70 - 75% of the RAM reasonably well though with a simple tester:
Install the memory tester:
sudo apt install memtester
Test 750MB of memory as follows. The "1" on the end means run once. You might want to increase this number to repeat the test a few times to catch more subtle problems.
sudo memtester 750MB 1
If it says "killed", you tried to test too much memory and the tester tried to allocated more memory than was allowed by the Raspberry PI O/S. Try testing slightly less and try again. Experiment with the amount such that you test as large a block of RAM as possible.
got 900MB (943718400 bytes), trying mlock ...Killed
The memory test will take some time to run and you will see output like this:
pagesize is 4096
pagesizemask is 0xfffffffffffff000
want 750MB (786432000 bytes)
got 750MB (786432000 bytes), trying mlock ...locked.
Loop 1/1:
Stuck Address : ok
Random Value : ok
Compare XOR : ok
Compare SUB : ok
Compare MUL : ok
Compare DIV : ok
Compare OR : ok
Compare AND : ok
Sequential Increment: ok
Solid Bits : ok
Block Sequential : ok
Checkerboard : ok
Bit Spread : ok
Bit Flip : testing 352
You should get "ok" against every test and eventually it will say "done" when complete.
A Raspberry Pi Zero I was testing with faulty RAM completely froze within seconds of starting the test and the green LED started flashing continuously. If you find a memory fault, there is not a lot you can do to fix it other than buy a new Pi as the memory cannot be removed.
Testing the CPUs
To test the CPU, just run any program that uses a lot of CPU and then run a copy on all the cores for a long time. Here's a simple Python program that just loads one core to 100% and runs forever. It does nothing useful, it just wastes CPU:
python3 -c $'a=99\nwhile True:\n a/=9.9' &
A Raspberry Pi currently typically has four CPU cores, so run the above line four times to fully load to Pi CPU to max. Leave it running for an hour or two and see if it crashes and also monitor the Pi temperature.
Monitoring the CPU temperature
When testing the CPUs, monitor the CPU temperature to determine if a heatsink/fan may be required. Your Pi may freeze due to temperature problems. If you intend to run you Pi in a case, install it before testing as the case may restrict airflow and significantly heat up the Pi. You might also want to wait for a particularly warm day to come round and re-test it to see how the weather affects the CPU temperature.
watch -n 0.5 vcgencmd measure_temp
- Anything under 70C should cause no problems and you can run it for long periods at this temperature.
- A Pi will generally work at 80C although this is a little hot and you might want to think about a cooling solution like a heatsink or/and fan.
- At or above 85C will almost certainly cause trouble for most Pis and should be avoided. The Pi will eventually start thermal throttling (slowing down) to try and cool itself but may ultimately freeze or reboot if the temperature does not decrease.
Image Credits: Raspberry Pi Zero, by Evan-Amos, Public Domain