Centos7 USES memory to optimize disk cache read and write speed

  • 2020-10-23 21:18:25
  • OfStack

The /dev/shm directory in Linux does not belong to disk, but to memory, and if you use /dev/shm/ directory as a read/write cache for disk files in Linux, you can imagine the efficiency is amazing.

The default /dev/shm directory is not mounted and needs to be mounted manually.

Add the following at the end of the following file:


$ vim /etc/fstab

tmps /dev/shm tmpfs defaults,size=1G 0 0

Please add according to your own physical memory size, 1 is generally about 10-50% of the physical memory.

Mount /dev/shm/ directory:


$ mount -o remount /dev/shm/
$ mkdir /dev/shm/tmp
$ chmod 755 /dev/shm/tmp
$ mount -B /dev/shm/tmp /tmp

Note:

/dev/shm/tmp will lose the mount after the system is rebooted, and the mount needs to be reset. There is an shell script below, and you can add it to boot:


$ vim /etc/init.d/shmtmp.sh

#!/bin/bash
mkdir /dev/shm/tmp
chmod 755 /dev/shm/tmp
mount -B /dev/shm/tmp/ /tmp

Then add the following at the end of the following file:


$ vim /etc/rc.local

sh /etc/init.d/shmtmp.sh

This enables the automatic mount restart. You can leverage memory to improve read and write performance, for example, by placing session of php and other caches in the /tmp directory and doubling the efficiency.


Related articles: