I have my raspberry pi A+ in my embeeded project and every byte of memory is important when I've only got 128 MB to my CPU (I need 128 MB for the GPU because I use the camera module). Unfortunently the memory is eaten up by linux quite quickly.
Freeing up the cache makes a huge difference
Before I ran a few commands on my raspberry pi I had less than 4 MB of memory left!
root@raspberrypi:/mnt/storage/archive# free
total used free shared buffers cached
Mem: 120544 116840 3704 0 4268 68612
-/+ buffers/cache: 43960 76584
Swap: 0 0 0
I found this article online and decided to give it a shot and run the commands they recommended and the result is mindblowing.
root@raspberrypi:/mnt/storage/archive# free
total used free shared buffers cached
Mem: 120544 64768 55776 0 396 21380
-/+ buffers/cache: 42992 77552
Swap: 0 0 0
Now I was back up to just about 56 MB of memory!
What might be the source
I am running several processes that communicate via UNIX sockets and I might just have missed a huge resource leak in one of my programs, but how would I detect it?
Furthermore since I want my project to be up and running pretty much all of the time I want to somehow detect when the memory is low (say < 10%) and then run these set of magic commands to free up my memory.
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
sync
I persume I have a resource leak in one of my applications but all I'm doing is communicating via UNIX sockets and an Arduino via serial using wiringPi. I've been very careful to cleanup all my memory that I allocate dynamically and it doesn't seem to be any issues with heap memory anyways since the cached memory seems to be what's giving me issues.