Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(It's probably a bit late for the OP, but this is asked quite often, so I'll give it a shot)</p> <p><code>free</code> normally shows something like this:</p> <pre><code> total used free shared buffers cached Mem: 8195284 8137708 57576 0 1232328 2651156 -/+ buffers/cache: 4254224 3941060 Swap: 18892216 759852 18132364 </code></pre> <p>People tend to look at the <code>Mem:</code> line when trying to find out how much free memory they have. Unfortunately that line is quite misleading, because the Linux kernel tries to make optimal use of the available memory in (at least) these ways:</p> <ul> <li><p>It will cache data from the I/O subsystem (e.g. the disk), so that it will be readily available if needed.</p></li> <li><p>It will actively evict processes that have been inactive for some time to the swap space, in favour of caching data for active processes. This tends to favour throughput over responsiveness, so some people tune their kernel to change this behaviour.</p></li> </ul> <p>The first point is the source of confusion regarding <code>free</code>, because the <code>Mem:</code> line includes the memory used for caching in the used memory amount. The kernel, though, will cache as much as possible for performance reasons. In fact, on any Linux system that has been up for some time, the free memory tends to be close to zero - unused memory is wasted memory.</p> <p>The cache memory, though, can be freed by the kernel if needed by another process. While it will impact I/O performance to a degree, other processes can have more memory <em>without using the swap space</em>. Therefore, for most intents and purposes, <em>that memory is free</em>.</p> <p>That's why <code>free</code> includes a second line, where the cache memory is considered free:</p> <pre><code>-/+ buffers/cache: 4254224 3941060 </code></pre> <p>This second line is what people should be looking at when they want to know if they have enough free memory for a certain purpose.</p> <p>In the example above, according to the <code>Mem:</code> line there are ~57 MB of free memory. If one reads the second line, though, there are in fact about 3.9 <em>GB</em> that can be used without forcing active processes to swap. As a sidenote, there are also about 760 MB of rarely-used data that have been swapped out, to make more space in the main memory for processes and caching.</p> <p>At roughly the same time, the contents of <code>/proc/meminfo</code>:</p> <pre><code>MemTotal: 8195284 kB MemFree: 57660 kB Buffers: 1232352 kB Cached: 2651156 kB SwapCached: 119936 kB . . . </code></pre> <p><code>MemTotal</code>: the available physical memory detected by the kernel.</p> <p><code>MemFree</code>: the unused physical memory - the free memory shown in the <code>Mem:</code> line of <code>free</code>.</p> <p><code>Buffers</code>: relatively temporary storage of raw disk blocks.</p> <p><code>Cached</code>: in-memory cache for files read from the disk. It does not include SwapCached memory.</p> <p><code>SwapCached</code>: memory that was once swapped out, then swapped back in but is still in the swap space. If needed, its contents can be just discarded (very fast!), without having to swap them out (slower).</p> <p>So, to have a semi-accurate estimate of the memory that is actually available</p> <pre><code>MemFree + Buffers + Cached + SwapCached </code></pre> <p>is a good starting point - and the one <code>free</code> shows in that second line.</p> <p>Naturally, memory management and the related statistics and measurements are more complicated than this. The numbers shown by <code>free</code> are mere estimates at best, since there are a lot of other variables to take into account if you want to go deeper. For people who regularly perform memory usage optimization, this is almost a form of art.</p> <p>EDIT:</p> <p>A somewhat humorous link about this "issue":</p> <p><a href="http://www.linuxatemyram.com/" rel="noreferrer">http://www.linuxatemyram.com/</a></p> <p>EDIT 2:</p> <p>To confirm the comment about memory use analysis almost being a form of art:</p> <p>Even <code>free</code> misses a major chunk of cached data on modern Linux systems. From <code>/proc/meminfo</code> on my system:</p> <pre><code>SReclaimable: 2253576 kB </code></pre> <p>That's about 2GB of memory that is used by the system <a href="http://en.wikipedia.org/wiki/Slab_allocation" rel="noreferrer">slab</a> allocator for caching directory entries and such and it is reclaimable (i.e. it can be cleared and used by processes if necessary). Yet <code>free</code> does not consider it cache memory and does not enter it in any of its calculations and therefore it shows up as used memory.</p> <p>The <code>slabtop</code> utility, if available, allows the system administrator to find out what the <a href="http://en.wikipedia.org/wiki/Slab_allocation" rel="noreferrer">slab</a> cache is used for.</p> <p>A way (for the root user only) to have <code>free</code> show the actual memory use of the system is the following:</p> <pre><code># swapoff -a # sync # echo 3 &gt; /proc/sys/vm/drop_caches # free total used free shared buffers cached Mem: 8195284 3181468 5013816 0 8656 228832 -/+ buffers/cache: 2943980 5251304 Swap: 0 0 0 # swapon -a </code></pre> <p>The first command disables the swap space. It should not be issued if the available memory may not be enough to hold the data that have been swapped out - in that case one has to take into account the <code>Swap:</code> line of free in their memory usage calculations.</p> <p>The second command pushes all buffered data to the disk. It allows more cache memory to be freed in the next step.</p> <p>The third command is the most important of the set - it forces the kernel to discard as much cached data as possible (page cache, directory entries, inodes etc).</p> <p>Then <code>free</code> finally shows what the running processes actually use in its <code>-/+ buffers/cache:</code> line. It is quite noticeable that even after dropping all cached data the kernel quickly starts caching again - in this case it has already reached almost 250MB of cached data within a few seconds.</p> <p>The final command enables the swap space again - it is only necessary if the first command was used too.</p> <p>It should be noted that these commands should be executed by the root user in order to have the necessary privileges.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload