2

On my Fedora 24, I executed two commands to examine the memory information, however, I got two different results about swap cache's size.

The first command is as follows:

[root@localhost ~]# vmstat -s
      4029004 K total memory
      1043700 K used memory
       742144 K active memory
       321916 K inactive memory
      2372500 K free memory
        63628 K buffer memory
       549176 K swap cache
      4063228 K total swap
            0 K used swap
      4063228 K free swap
      ......

The second command is as follows:

[root@localhost ~]# cat /proc/meminfo
MemTotal:        4029004 kB
MemFree:         2369712 kB
MemAvailable:    2690460 kB
Buffers:           63720 kB
Cached:           437460 kB
SwapCached:            0 kB
......

Please note: vmstat -s says 549176 K swap cache, while cat /proc/meminfo says SwapCached: 0 kB.

My question are:

  1. What does it mean by swap cache?
  2. What does it mean by SwapCached?
  3. Are they the same thing? If no, what's the difference between them?
2

In vmstat the field cache is the sum of cat /proc/meminfo fields Cached and Slab.

  • It means it's approximately file cache plus inode cache plus directory cache plus caches for extra objects.
  • The same field is called swap cache in vmstat -s.
  • The field has nothing, zero, nil to do with swap. A misnomer, as it often happens.
  • To calculate this from cat /proc/vmstat fields, use:

swap cache = nr_file_page + nr_slab_reclaimable + nr_slab_unreclaimable - meminfo_SwapCached

Looking at /proc/meminfo, the field SwapCached is unrelated; it has nothing to do with any disk cache or slab cache. It's how much of the swap is also present in the clean memory pages.

It seems the only counter which mixes up file cache with swap is the nr_file_page (it treats swap as any other another "cached" file; this terminology is totally incompatible with other counters/tools).

| improve this answer | |
1

There are two commands and two files.

]# free -V; vmstat -V
free    from procps-ng 3.3.15
vmstat  from procps-ng 3.3.15

Proc-Files: meminfo and vmstat

Free's manpage explains about meminfo fields. Some of these fields are also calculated indirectly.

vmstat only adds some /proc/vmstat fields.

/proc/vmstat has a more systematic (and longer) structure. Here are four lines where that mysterious pagecache is mentioned

nr_anon_pages   212687
nr_mapped        70247
nr_file_pages   887005
nr_dirty            64

Now these strings are very weak compared to the enum list in mmzone.h, with comments:

NR_ANON_MAPPED, /* Mapped anonymous pages */
NR_FILE_MAPPED, /* pagecache pages mapped into pagetables. only modified from process context */
NR_FILE_PAGES,
NR_FILE_DIRTY,

(In first place come the 4 lines ANON and FILES combined with ACTIVE and INACTIVE)


So your second one is not a command, but the contents of the proc file the commands rely on.


fs/proc/meminfo.c:

    cached = global_node_page_state(NR_FILE_PAGES) -
                    total_swapcache_pages() - i.bufferram;
    if (cached < 0)
            cached = 0;

This illustrates there is no own "cached" counter, and it has to be sanity checked. It is basically all the "file pages", active or inactive, with some corrections.

i.buffers is added back in the 'buff/cache' field of free

This part (/proc/meminfo contents) shows how the values are collected together. i is a struct sysinfo. cached and available are calculated right before. The pages[] array is very like /proc/vmstat, also Mlocked with that zone page call.

    show_val_kb(m, "MemTotal:       ", i.totalram);
    show_val_kb(m, "MemFree:        ", i.freeram);
    show_val_kb(m, "MemAvailable:   ", available);
    show_val_kb(m, "Buffers:        ", i.bufferram);
    show_val_kb(m, "Cached:         ", cached);
    show_val_kb(m, "SwapCached:     ", total_swapcache_pages());
    show_val_kb(m, "Active:         ", pages[LRU_ACTIVE_ANON] +
                                       pages[LRU_ACTIVE_FILE]);
    show_val_kb(m, "Inactive:       ", pages[LRU_INACTIVE_ANON] +
                                       pages[LRU_INACTIVE_FILE]);
    show_val_kb(m, "Active(anon):   ", pages[LRU_ACTIVE_ANON]);
    show_val_kb(m, "Inactive(anon): ", pages[LRU_INACTIVE_ANON]);
    show_val_kb(m, "Active(file):   ", pages[LRU_ACTIVE_FILE]);
    show_val_kb(m, "Inactive(file): ", pages[LRU_INACTIVE_FILE]);
    show_val_kb(m, "Unevictable:    ", pages[LRU_UNEVICTABLE]);
    show_val_kb(m, "Mlocked:        ", global_zone_page_state(NR_MLOCK));

swap cache is what man free calls cache:

cache: Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)

Because of (simplified, see above):

cached = NR_FILE_PAGES - total_swapcache_pages()

...there is a interesting relationship with SwapCached. If only the names were less confusing.


What swap cache is for files on disk, SwapCached is for ANON pages on a swap device. (I hope this makes sense/is not totally wrong)

| improve this answer | |

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.