How to drop kernel caches

Free dirty objects, page cache and reclaimable slab objects for troubleshooting or instructional purposes.

Free dirty objects

Use sync utility to synchronize memory waiting to be written back to the persistent storage.

$ sync

Inspect /proc/meminfo to observe that Dirty memory that is waiting to be written back to the disk (defined using kilobytes) will decrease. You can also notice a short surge of the Writeback memory which is actively being written back to the persistent storage and WritebackTmp which is directly related to FUSE (Filesystem in Userspace).

Changes in memory usage before and after issuing this command.

MemTotal:          16287088 kB             (
MemFree:            7688696 kB             | MemFree:         7713352 kB
MemAvailable:       8315048 kB             | MemAvailable:    8339800 kB
Buffers:              18452 kB             | Buffers:           18492 kB
Cached:             1836208 kB             | Cached:          1836268 kB
SwapCached:               0 kB             (
Active:             6457040 kB             | Active:          6430504 kB
Inactive:           1459164 kB             | Inactive:        1459204 kB
Active(anon):       6070180 kB             | Active(anon):    6043584 kB
Inactive(anon):     1021284 kB             (
Active(file):        386860 kB             | Active(file):     386920 kB
Inactive(file):      437880 kB             | Inactive(file):   437920 kB
Unevictable:             32 kB             (
Mlocked:                 32 kB             (
SwapTotal:           999420 kB             (
SwapFree:            999420 kB             (
Dirty:                21200 kB             | Dirty:                 0 kB    ←
Writeback:                0 kB             (
AnonPages:          6061728 kB             | AnonPages:       6035384 kB
Mapped:             1248224 kB             | Mapped:          1248260 kB
Shmem:              1029928 kB             (
Slab:                348728 kB             | Slab:             348748 kB
SReclaimable:        120864 kB             | SReclaimable:     120852 kB
SUnreclaim:          227864 kB             | SUnreclaim:       227896 kB
KernelStack:          29168 kB             | KernelStack:       29184 kB
PageTables:           91616 kB             | PageTables:        91656 kB
NFS_Unstable:             0 kB             (
Bounce:                   0 kB             (
WritebackTmp:             0 kB             (
CommitLimit:        9142964 kB             (
Committed_AS:      22529316 kB             | Committed_AS:   22536664 kB
VmallocTotal:   34359738367 kB             (
VmallocUsed:              0 kB             (
VmallocChunk:             0 kB             (
HardwareCorrupted:        0 kB             (
AnonHugePages:        38912 kB             (
ShmemHugePages:           0 kB             (
ShmemPmdMapped:           0 kB             (
CmaTotal:                 0 kB             (
CmaFree:                  0 kB             (
HugePages_Total:          0                (
HugePages_Free:           0                (
HugePages_Rsvd:           0                (
HugePages_Surp:           0                (
Hugepagesize:          2048 kB             (
DirectMap4k:         658988 kB             (
DirectMap2M:       11786240 kB             (
DirectMap1G:        5242880 kB 

Free page cache

Use sysctl utility to issue command to the kernel to invalidate page cache which is the main disk cache used when data is either read from disk or written to disk.

$ sudo sysctl -w vm.drop_caches=1

Inspect /proc/meminfo to observe that page cache in memory will decrease.

Changes in memory usage before and after issuing this command.

MemTotal:          16287088 kB             (
MemFree:            1595736 kB             | MemFree:         7079620 kB
MemAvailable:       8339004 kB             | MemAvailable:    8325900 kB
Buffers:            1714248 kB             | Buffers:            1088 kB    ←
Cached:             5524636 kB             | Cached:          1853228 kB    ←
SwapCached:               0 kB             (
Active:             8972220 kB             | Active:          6319944 kB
Inactive:           4226696 kB             | Inactive:        1496236 kB
Active(anon):       5963252 kB             | Active(anon):    5962128 kB
Inactive(anon):     1063684 kB             | Inactive(anon):  1076596 kB
Active(file):       3008968 kB             | Active(file):     357816 kB    ←
Inactive(file):     3163012 kB             | Inactive(file):   419640 kB    ←
Unevictable:             32 kB             (
Mlocked:                 32 kB             (
SwapTotal:           999420 kB             (
SwapFree:            999420 kB             (
Dirty:                  296 kB             | Dirty:               968 kB 
Writeback:                0 kB             (
AnonPages:          5955404 kB             | AnonPages:       5957264 kB
Mapped:             1250196 kB             | Mapped:          1250348 kB
Shmem:              1066912 kB             | Shmem:           1076720 kB
Slab:               1149756 kB             | Slab:            1047280 kB
SReclaimable:        914580 kB             | SReclaimable:     812116 kB
SUnreclaim:          235176 kB             | SUnreclaim:       235164 kB
KernelStack:          29200 kB             | KernelStack:       29104 kB
PageTables:           91272 kB             | PageTables:        91288 kB
NFS_Unstable:             0 kB             (
Bounce:                   0 kB             (
WritebackTmp:             0 kB             (
CommitLimit:        9142964 kB             (
Committed_AS:      22527888 kB             | Committed_AS:   22538996 kB
VmallocTotal:   34359738367 kB             (
VmallocUsed:              0 kB             (
VmallocChunk:             0 kB             (
HardwareCorrupted:        0 kB             (
AnonHugePages:        38912 kB             (
ShmemHugePages:           0 kB             (
ShmemPmdMapped:           0 kB             (
CmaTotal:                 0 kB             (
CmaFree:                  0 kB             (
HugePages_Total:          0                (
HugePages_Free:           0                (
HugePages_Rsvd:           0                (
HugePages_Surp:           0                (
Hugepagesize:          2048 kB             (
DirectMap4k:         658988 kB             (
DirectMap2M:       11786240 kB             (
DirectMap1G:        5242880 kB             (

Free reclaimable slab objects

Use sysctl utility to issue command to the kernel to free reclaimable slab objects including dentries and inodes.

$ sudo sysctl -w vm.drop_caches=2

Inspect /proc/meminfo to observe that SReclaimable memory allocated for internal kernel data structures that can be reclaimed will decrease.

Inspect /proc/slabinfo for detailed information on inode, dentry caches.

Changes in memory usage before and after issuing this command.

MemTotal:          16287088 kB             (
MemFree:            7078272 kB             | MemFree:         7827464 kB
MemAvailable:       8329644 kB             | MemAvailable:    8411084 kB
Buffers:               2564 kB             | Buffers:            3696 kB
Cached:             1843068 kB             | Cached:          1804908 kB
SwapCached:               0 kB             (
Active:             6334520 kB             | Active:          6341572 kB
Inactive:           1484256 kB             | Inactive:        1438124 kB
Active(anon):       5973268 kB             | Active(anon):    5979840 kB
Inactive(anon):     1062816 kB             | Inactive(anon):  1016812 kB
Active(file):        361252 kB             | Active(file):     361732 kB
Inactive(file):      421440 kB             | Inactive(file):   421312 kB
Unevictable:             32 kB             (
Mlocked:                 32 kB             (
SwapTotal:           999420 kB             (
SwapFree:            999420 kB             (
Dirty:                 1100 kB             | Dirty:              1568 kB
Writeback:                0 kB             (
AnonPages:          5968564 kB             | AnonPages:       5971180 kB
Mapped:             1249996 kB             | Mapped:          1248236 kB
Shmem:              1062948 kB             | Shmem:           1025484 kB
Slab:               1047264 kB             | Slab:             346196 kB
SReclaimable:        811972 kB             | SReclaimable:     118788 kB     ←
SUnreclaim:          235292 kB             | SUnreclaim:       227408 kB
KernelStack:          29120 kB             | KernelStack:       29136 kB
PageTables:           91416 kB             | PageTables:        91560 kB
NFS_Unstable:             0 kB             (
Bounce:                   0 kB             (
WritebackTmp:             0 kB             (
CommitLimit:        9142964 kB             (
Committed_AS:      22508796 kB             | Committed_AS:   22493648 kB
VmallocTotal:   34359738367 kB             (
VmallocUsed:              0 kB             (
VmallocChunk:             0 kB             (
HardwareCorrupted:        0 kB             (
AnonHugePages:        38912 kB             (
ShmemHugePages:           0 kB             (
ShmemPmdMapped:           0 kB             (
CmaTotal:                 0 kB             (
CmaFree:                  0 kB             (
HugePages_Total:          0                (
HugePages_Free:           0                (
HugePages_Rsvd:           0                (
HugePages_Surp:           0                (
Hugepagesize:          2048 kB             (
DirectMap4k:         658988 kB             (
DirectMap2M:       11786240 kB             (
DirectMap1G:        5242880 kB             (

Changes in kernel slab allocator statistics before and after issuing this command.

$ sdiff -l -w 57 slabinfo.20 slabinfo.21
slabinfo - version: 2.1     (
# name            <active   (
nfs_direct_cache        0    (
nfs_read_data          68    (
nfs_inode_cache       102    | nfs_inode_cache      100     ←
fscache_cookie_jar      2    (
nf_conntrack         1727    | nf_conntrack        1752 
ovl_inode           43303    | ovl_inode           3096     ←
au_finfo                0    (
au_icntnr               0    (
au_dinfo                0    (
kvm_async_pf            0    (
kvm_vcpu                0    (
kvm_mmu_page_header          (
pte_list_desc           0    (
rpc_inode_cache        41    (
ext4_groupinfo_4k    7454    (
drm_i915_gem_request    9    (
i915_vma             2954    | i915_vma            2953 
drm_i915_gem_object   292    | drm_i915_gem_object  292
RAWv6                 464    (
UDPv6                 260    (
tw_sock_TCPv6          34    (
request_sock_TCPv6      0    (
TCPv6                 135    (
kcopyd_job              0    (
dm_uevent               0    (
cfq_io_cq             476    (
cfq_queue             510    (
bsg_cmd                 0    (
mqueue_inode_cache     62    (
fuse_request           67    (
fuse_inode             98    | fuse_inode            76     ←
ecryptfs_inode_cache         (
ecryptfs_auth_tok_list_it    (
fat_inode_cache        19    | fat_inode_cache        3     ←
fat_cache               0    (
squashfs_inode_cache  188    | squashfs_inode_cache  87     ←
jbd2_journal_head    1938    (
jbd2_revoke_table_s    25    (
ext4_inode_cache   469038    | ext4_inode_cache    7998     ←
ext4_allocation_context      (
ext4_io_end          1728    (
ext4_extent_status 244698    | ext4_extent_status 10710
mbcache              1898    (
fscrypt_info         1152    (
fscrypt_ctx           680    (
userfaultfd_ctx_cache        (
dio                   205    (
pid_namespace         312    (
posix_timers_cache    140    (
ip4-frags              23    (
xfrm_dst_cache          0    (
RAW                   776    | RAW                  896 
tw_sock_TCP            68    (
request_sock_TCP       52    (
TCP                   240    (
hugetlbfs_inode_cache        (
dquot                 256    (
eventpoll_pwq        6716    (
eventpoll_epi        8056    (
dax_cache               4    (
request_queue          40    (
dmaengine-unmap-256          (
dmaengine-unmap-128          (
sock_inode_cache     7046    (
file_lock_cache       321    (
net_namespace          33    (
shmem_inode_cache    7808    | shmem_inode_cache   7807     ←
taskstats             879    (
proc_inode_cache    20991    | proc_inode_cache    7381     ←
sigqueue              200    (
bdev_cache            105    (
kernfs_node_cache 146133     | kernfs_node_cache 141254 
mnt_cache            4087    (
filp                43510    | filp               44230 
inode_cache         34643    | inode_cache        23641     ←
dentry             736367    | dentry             53630     ←
iint_cache              0    (
lsm_file_cache      24003    | lsm_file_cache     24002 
iint_cache              0    (
buffer_head         15177    | buffer_head        15417 
nsproxy              1920    | nsproxy             1577 
vm_area_struct      86939    | vm_area_struct     87250 
mm_struct            1353    (
files_cache          1584    (
signal_cache         2357    (
sighand_cache        1434    (
task_struct          2364    | task_struct         2356 
cred_jar            11118    | cred_jar           11706 
anon_vma            41724    | anon_vma           41769 
pid                 74665    | pid                74853 
Acpi-Operand        11627    (
Acpi-ParseExt         624    (
Acpi-State           1377    (
Acpi-Namespace       6936    (
numa_policy            31    (
trace_event_file     1970    (
ftrace_event_field  37941    | ftrace_event_field 17566
radix_tree_node     19962    | radix_tree_node    19578 
task_group            420    (
dma-kmalloc-8192        0    (
dma-kmalloc-4096        0    (
dma-kmalloc-2048        0    (
dma-kmalloc-1024        0    (
dma-kmalloc-512         0    (
dma-kmalloc-256         0    (
dma-kmalloc-128         0    (
dma-kmalloc-64          0    (
dma-kmalloc-32          0    (
dma-kmalloc-16          0    (
dma-kmalloc-8           0    (
dma-kmalloc-192         0    (
dma-kmalloc-96          0    (
kmalloc-8192          607    | kmalloc-8192         588 
kmalloc-4096         2803    | kmalloc-4096        2768 
kmalloc-2048         5914    | kmalloc-2048        5749 
kmalloc-1024        12056    | kmalloc-1024       12030 
kmalloc-512         13772    | kmalloc-512        13765 
kmalloc-256         10834    | kmalloc-256        10775 
kmalloc-192         11681    | kmalloc-192        11147 
kmalloc-128         10545    | kmalloc-128         7556 
kmalloc-96          24952    | kmalloc-96         16292 
kmalloc-64         144963    | kmalloc-64         43284 
kmalloc-32         170695    | kmalloc-32        166264 
kmalloc-16          32519    (
kmalloc-8           22539    (
kmem_cache_node      4544    (
kmem_cache           4347    | kmem_cache          3654 

Free page cache and reclaimable slab objects

Use sysctl utility to issue command to the kernel to free page cache and reclaimable slab objects including dentries and inodes.

$ sudo sysctl -w vm.drop_caches=3

Disable logging messages

Issuing drop cache command will emit KERN_INFO level log message.

$ sudo sysctl -w vm.drop_caches=2
$ dmesg
[...]
[Wed Sep  5 03:02:39 2018] sysctl (12414): drop_caches: 2
[...]
$ echo 1 | sudo tee /proc/sys/vm/drop_caches
$ dmesg
[...]
[Wed Sep  5 03:05:38 2018] tee (12696): drop_caches: 1
[...]

This log message can be disabled, although this action will be logged.

$ sudo sysctl -w vm.drop_caches=4
$ dmesg
[...]
[Wed Sep  5 03:08:39 2018] sysctl (12730): drop_caches: 4
[...]

This will prevent further log messages, but it cannot be switched back on again without system reboot, after which it will resume standard logging operations.