Linux understand Page cache and buffer cache

In this tutorial, I have written about Page cache and buffer cache in Linux System.

Most file-system cache data read from disk.

What does Page cache work?

A cache of data accessed via the file system.

How to check page cache is actually used.

Create a large file

[root@DevopsRoles ~]# mkdir /test
[root@DevopsRoles ~]# dd if=/dev/zero of=/test/large.txt count=100 bs=10M
100+0 records in
100+0 records out
1048576000 bytes (1.0 GB) copied, 1.62731 s, 644 MB/s
[root@DevopsRoles ~]# echo 3 > /proc/sys/vm/drop_caches

Check memory usage before putting it in the page cache

[root@DevopsRoles ~]# vmstat
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  1  0      0 377344      0  64336    0    0   441  5203  131  293  0  2 97  0  0

[root@DevopsRoles ~]# cat /test/large.txt > /dev/null

Check memory usage after getting on page cache

[root@DevopsRoles ~]# vmstat 
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0   6500      0 435008    0    0  3265  3008  108  179  0  2 98  0  0

We will confirm that accessing data in the cache is fast.

Run command 1s

[root@DevopsRoles ~]# time cat /test/large.txt > /dev/null

real	0m1.068s
user	0m0.003s
sys	0m0.987s

Run command 2s

[root@DevopsRoles ~]# time cat /test/large.txt > /dev/null

real	0m1.064s
user	0m0.003s
sys	0m0.981s

What does Buffer cache work?

Cache data accessed via raw I/O. It is a page cache for block devices.

How to check Buffer cache is actually used.

[root@DevopsRoles ~]# vmstat
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  1  0      0   5668      0 435832    0    0  6434  2087  116  132  0  2 98  0  0
 [root@DevopsRoles ~]# dd if=/dev/sda of=/dev/null count=100 bs=10M
 100+0 records in
 100+0 records out
 1048576000 bytes (1.0 GB) copied, 1.59043 s, 659 MB/s

Increase buffer cache (buff)

We will confirm that accessing data in the cache is fast.

Run command 1s

[root@DevopsRoles ~]# vmstat
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  2  0      0  13408 359528  68520    0    0  7715  1888  123  127  0  2 98  0  0
 [root@DevopsRoles ~]# time dd if=/dev/sda of=/dev/null count=100 bs=10M
 100+0 records in
 100+0 records out
 1048576000 bytes (1.0 GB) copied, 1.13208 s, 926 MB/s
 real    0m1.138s
 user    0m0.001s
 sys    0m1.068s

Run command 2s

[root@DevopsRoles ~]# vmstat
 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  2  0      0  13664 359220  68520    0    0  8896  1747  130  119  0  2 98  0  0
 [root@DevopsRoles ~]# time dd if=/dev/sda of=/dev/null count=100 bs=10M
 100+0 records in
 100+0 records out
 1048576000 bytes (1.0 GB) copied, 1.13821 s, 921 MB/s
 real    0m1.144s
 user    0m0.001s
 sys    0m1.072s

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment moderation is enabled. Your comment may take some time to appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.