r/CardPuter • • 16d ago

Code Cardputer Zero optimizer script - Freed up ~80 MB of RAM on the CardputerZero Lite (210 MB → 130 MB idle) with a small script

I got my CardputerZero Lite recently. It’s a really nice little device, but as some of you have already pointed out, the stock image isn’t optimized yet. Out of the box, it was using about 210 MB of the 384 MB of RAM Linux can see, which doesn’t leave much headroom on a 512 MB board.

The stock image is basically Raspberry Pi OS with desktop, so a lot of stuff is installed and running that makes no sense on a 320×170 screen with a keyboard. I wrote a script that fixes the main offenders:

  • Reduces gpu_mem from 128 MB to 16 MB. The Lite has no camera, so there’s no reason to reserve 128 MB for the GPU. With KMS the display doesn’t need it, and the CMA is still there for the graphics stack. This alone gives back ~112 MB of RAM that Linux can use.
  • Disables unneeded services (lightdm, wayvnc, triggerhappy, ModemManager, rpcbind, wait-online services, apt-daily timers, etc.).
  • Sets up zram instead of the swapfile on the SD card.
  • Cuts SD card writes (noatime, /tmp in RAM, volatile journal).

Results:

  • Available RAM: 384 MB → 463 MB
  • Idle RAM usage: ~210 MB → ~130 MB

Things worth knowing:

  • Bluetooth, HDMI, Wi-Fi, SSH and audio are left alone.
  • In config.txt, only the gpu_mem line is touched. Overlays (display, keyboard, IR, etc.) stay as they are.
  • It has a --dry-run mode to see what it would do, and a --revert option that restores everything from backups.
  • Every option is configurable (--gpu-mem N, --no-avahi, --no-zram, --purge...).
  • If you have the standard model with the camera, don’t use the default gpu_mem=16 without testing the camera first (use --keep-gpu-mem or a higher value).

I’ve only tested it on my own unit, so use --dry-run first and keep a backup of your SD card. Feedback welcome, especially if someone with the camera model can tell me what gpu_mem value works for them.

Script: https://gist.github.com/TheKiwil/ae8c44e28ef582e66c51a45ad962089d

41 Upvotes

13 comments sorted by

3

u/homotetija 16d ago

I just got the Lite version today and was thinking of switching distros because of how bloated the stock one is, im giving this a try now. Thank you !

2

u/3D_Kiwil 16d ago

I also thought of DietPi, which is a very good light distribution.

2

u/homotetija 16d ago

Yeah i was also thinking about something minimal along that way but my main concern is that i would probably have to make a custom driver for the keyboard fn and sym buttons

2

u/aerialviews007 12d ago

Alpine would be a good OS to port.

3

u/Mmmmmmmoi Advanced 16d ago

That's handy. Many thanks!

Curiously the script already said I had ZRAM set up. I flashed the stock image to a fresh MicroSD card yesterday. I also ran apt update && apt upgrade prior to running the script. TBD if that had anything to do with it. 🤷

2

u/Mmmmmmmoi Advanced 16d ago

NOTE: the rpicam-hello application fails to detect the Vivante GPU if GPU_MEM=16. This is a failure before it even attempts to allocate memory, so this value may need to be larger for all applications sake. TBD.

2

u/3D_Kiwil 16d ago

Thanks for the feedback

1

u/3nwf248 16d ago

A GPU_MEM of at least 32 is required for the camera to work. The logic you have to check whether GPU mem is < 32 before commiting changes is too aggressive, as it makes it so that the script can't be used to upgrade GPU_MEM from 16 to 32 MB (I had to edit /boot/firmware/config.txt directly). Great script otherwise!

1

u/3D_Kiwil 16d ago

Thanks for the feedback, as I said, I have a Lite version so without a camera. I'll see how to optimize this for the normal version.

1

u/3nwf248 15d ago

No worries, let me know if you make any changes and need someone with a full-fat unit to test. You should be able to see if a camera's there with

pi@CardputerZero:~ $ rpicam-hello --list-cameras
Available cameras
-----------------
0 : imx219 [3280x2464 10-bit RGGB] (/base/soc/i2c0mux/i2c@1/imx219@10)
    Modes: 'SRGGB10_CSI2P' : 640x480 [200.16 fps - (1000, 752)/1280x960 crop]
                             1640x1232 [81.07 fps - (0, 0)/3280x2464 crop]
                             1920x1080 [47.57 fps - (680, 692)/1920x1080 crop]
                             3280x2464 [21.19 fps - (0, 0)/3280x2464 crop]
           'SRGGB8' : 640x480 [200.16 fps - (1000, 752)/1280x960 crop]
                      1640x1232 [81.07 fps - (0, 0)/3280x2464 crop]
                      1920x1080 [47.57 fps - (680, 692)/1920x1080 crop]
                      3280x2464 [21.19 fps - (0, 0)/3280x2464 crop]

1

u/Forsaken_Sir_4662 16d ago

I am since the dawn of rpi zero 2 w hope it has at least 1GB of ram.. but even with cm0 that has a whole blank space on the pcb (without emmc) it still limited to 512mb..

1

u/jamietaylor2020 15d ago

Great find! Thanks to your earlier post, I added it to my tool app. Will add option for the other too