Let's talk a bit about the way how Linux uses memory.
So Linux places as many files as possible in cache to guarantee fast access to the files.
Cache is a convenience.
It doesn't have to be there, but if you can get a file from cache, it is obtained from
RAM instead of disk, and that will make servicing the file a lot faster.
Now because of this, Linux memory often shows as saturated, and when you use the appropriate
utilities, you might see that a very low amount of memory is available.
Another part of memory that is important is swap.
Swap is used as an overflow buffer of emulated RAM on disk, and the nice thing about swap
is that the Linux kernel moves inactive application memory to swap first, and inactive cache memory
will just be dropped if there's a shortage of memory, but because of the usage of swap,
Linux can use memory more efficiently as long as your applications support using swap.
You can use 3-M to get details about current memory usage, and more detailed memory information
is in proc meminfo.
Let me show you.
So when you type 3-M, you get an overview of memory, and the overview of memory is in
megabytes.
So here we can see the total amount of memory, the amount of memory that is used, and the
amount of memory that is free.
Now the interesting thing is that you also have the amount of memory that is available.
Why is there such a big difference between free memory and available?
Well, free memory is about RAM that is really not used by anything at the moment.
Available memory is a little bit smarter look on the situation where the Linux kernel is
considering memory that is currently used, but doesn't really have to be used, like inactive
cache memory.
And the idea is that if there is suddenly a huge demand for memory, then the Linux kernel
can drop inactive cache, and if inactive cache is considered as well, then you get the amount
of available memory.
Now BurfiSan cache is giving that currently 1.3 gigabytes approximately is used for caching
and for buffering, which is temporary memory use, and that is the memory that can be freed
up easily.
You also see swap information.
So I have two gigs in total, and of the two gigs, only seven megabytes is currently being
used.
That's almost nothing, so that doesn't matter too much.
If you want a more detailed information about memory, you can have a look at the PROC mem
info file.
PROC is an interesting file system because it's an interface to the kernel, and if ever
you want to investigate what is going on in your system, well, check in the PROC file
system for what you want to know.
And as you can see, this is giving very detailed information about different parts of memory
usage, and the part that I care about is highlighted here, active versus inactive, where I can
see that we have active file cache, 600 megabytes, and inactive is almost half a gig, and that's
the amount of memory that can be dropped in case there's a memory shortage.
Likewise for anonymous, that's your application memory.
So I have this amount of application memory that is actively used, and the applications
have claimed 831 megabytes of memory that is not actively used, and this memory is a
perfect candidate for moving to swap if a memory shortage arrives.
Let me give you a quick overview in a drawing to better understand how all of this is working.
Okay, let me make a drawing to explain a bit more how memory is used.
Let's say this is RAM.
Now you need to know what exactly is happening in RAM.
Well, RAM is used for caching or buffer.
It's the same approach, and we have application memory.
If you start an application, the application must be loaded in RAM.
There's also kernel memory, but that's not really something that you can optimize because it needs to be in RAM.
Now the thing is, what are you going to do if there's a shortage of memory?
Well, if there's a shortage of memory, then you could monitor inactive versus active memory.
And the thing is, if you get inactive application memory, you get it in swap.
Now swap on Linux is really elegant because the Linux kernel is first going to tune that your inactive application memory is going away,
and then you get more memory for cache and for buffers.
And if you have more for cache, that means that your reads are faster.
If you have more for buffers, that means that your writes are more efficient.
But if you are running out of memory here, well, the Linux kernel is just going to drop the inactive cache and buffers.
After all, this is just for optimization, and it's not really a requirement.
I'd also like to tell you a little bit about the write cache.
Write cache is what is used when you write a file.
And it's important that you understand how it is working because things can go wrong if you don't really understand what is going on.
So when you are writing files, a write cache is used.
And the write cache, also known as buffers, is an area in memory.
Now writing your files to the write cache is convenient for your application because once it has landed in the write cache,
the kernel can tell the application that it can continue.
But there is one thing that you should realize,
and that is that the write cache is periodically committed to disk by the pdflush kernel thread.
That's a part of the Linux kernel.
And as a result, after committing a file write, it's not immediately secure.
And if you want to ensure that the file is committed to disk immediately, you can use the sync command.
This is particularly useful if you see that the command is giving an amazing fast result.
Sometimes that happens.
And if you think this can't be true, then probably it isn't because it may mean that the data has not really been committed to the device.
It's still sitting in the write cache, and you need to sync it.
So if you're not sure about it, sync your data to make sure that it is actually committed to disk.