What data remains in page cache on Linux and how to remove it with one command quickly ?

In this post, we will at an interesting topic what data get stored in page cache and how to remove it quickly with one command.

To understand this lets take an example below.

1. Lets create a sample file called “abc9” and a hard link to the file called “abc9link”

[root@nglinux deleteperm]# echo "hello" > abc9 ; ln -s abc9 abc9link
[root@nglinux deleteperm]# cat abc9 abc9link 
hello
hello

2. Now lets overwrite the file’s content by zeros using deleteperm v1 command.(the command will be launched shortly and the issue is now resolved)

[root@nglinux deleteperm]# deleteperm abc9
[root@nglinux deleteperm]# 

3.Even after replacing the file blocks with zeros, lets have a look at the contents of the file.

[root@nglinux deleteperm]# cat abc9link 
hello

We can see the content is still visible with the hard link, since the data is picked from the page cache.

We can see the page cache using the “free -m” command.

4. Viewing Page Cache Size

[root@nglinux deleteperm]# free -m
             total       used       free     shared    buffers     cached
Mem:          1006        295        710          1         12        126
-/+ buffers/cache:        156        849
Swap:            0          0          0

We can see total 156 MB of page cache is saved on disk.

5. Removing the page cache.
Now lets remove the page cache to reflect the changes.

[root@nglinux deleteperm]# sysctl -w vm.drop_caches=3
vm.drop_caches = 3
[root@nglinux deleteperm]# 

[root@nglinux deleteperm]# free -m
             total       used       free     shared    buffers     cached
Mem:          1006        191        814          1          0         37
-/+ buffers/cache:        154        852
Swap:            0          0          0

6. Try to view the file content again.
Now if you try to see the file content, we can see there is nothing, since the data is already overwritten by zeros and the page cache is also cleared.

[root@nglinux deleteperm]# cat abc9link 
[root@nglinux deleteperm]# 

Seems interesting how our page cache saved the file content and serves user request from cache.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments