2

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.

6

Before I ran a few commands on my raspberry pi I had less than 4 MB of memory left!

Wrong. You had 76.5 MB left.

             total       used       free     shared    buffers     cached
 Mem:        120544    116840       3704          0      4268      68612
 -/+ buffers/cache:     43960      76584
                                   ^^^^^

Please read and do your best to understand the wikipedia article on page caching. Do not pass go otherwise. All contemporary operating systems do this and have for a long time, because for a long time it would be considered idiotic not to do so. In a nutshell, based on a few sequential premises:

  • Any memory not committed to user applications should be exploited by the operating system, if this will result in an appropriate performance enhancement. Put another way, if all you need is 10% of your RAM and the OS can find a good reason to use the other 90%, it should do so.

  • An ideal purpose for this "free" memory is to just hang onto whatever it was used for, if anything, in case that stuff is needed again in the near future.

  • A very significant percentage of memory (say, between one and two thirds) is often occupied by shared, read-only stuff, aka. file pages. For example, that executable program in /bin must be loaded into memory as is, and most of it probably won't change, meaning even if it is being used N times simultaneously only 1 copy of it need exist in memory.

  • Memory that isn't actually required by anything is always disposable, but can still contain data which can be kept track of.

  • Memory that was used for something no longer required can be left as is until needed, prioritizing the re-use of its contents. This is a restatement of the second point, and note it does not mean the memory is unavailable to be used for any purpose what-so-ever, is just means it contains cached content that could be reused. Which, following the third point, is often the case; the same copy of read-only code can be used by N processes where N is > 0, then N = 0, then N is > 0 again.

Hopefully this makes clear the importance of the page cache, and why total goofball idiots such as the one you linked from here are well, total goofball idiots for presenting this kind of information in an inappropriately context-free way (another WP article worth reading: the cargo cult) . I've been as rude as I can about that, but it is not your fault for falling for this. I think most people would require some formal education in computer science to not go down what seems to be a good kind of rabbit-hole, conceptually.

But it isn't. It's the other kind, best forgotten. This is a potentially "shoot yourself in the foot" environment but there's no good reason for you to do so. Quoting the official docs, where "this file" refers to /proc/sys/vm/drop_caches:

This file is not a means to control the growth of the various kernel caches (inodes, dentries, pagecache, etc...) These objects are automatically reclaimed by the kernel when memory is needed elsewhere on the system.

Use of this file can cause performance problems. Since it discards cached objects, it may cost a significant amount of I/O and CPU to recreate the dropped objects, especially if they were under heavy use. Because of this, use outside of a testing or debugging environment is not recommended.

So, that has been stated very clearly a number of times. Moving on, the reason you think this matters is because of the cached value, which is more or less the kernel page cache. Let's look at the second set of stats again.

             total       used       free     shared    buffers     cached
 Mem:        120544     64768      55776          0       396      21380
 -/+ buffers/cache:     42992      77552
                                   ^^^^^

Wow, you improved that by < 1 MB, which could happen as a matter of course anyway in the time it took to enter the command twice. That "drop caches" trick didn't really gain you anything. In fact, it only made things worse.

  • Thank you goldilocks for such a thourough answer, I clearly lack understanding in this subject and now atleast I think I have somewhat a better picture of page caching and what it is. I did get some out of memory problems on my process at one instance but that is unrelated. Thank you again for the time you put in to help an amateur out :) – Linus Nov 5 '15 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.