Use memory as a hard drive to speed up your linux system

 

Application scenarios
The memory of the computer is too large and can't be used as much, and it is wasted empty, so make use of it. The high memory read and write speed is used for caching or compiling, and it is even better to store temporary files~~

There is a name under Windows called a memory disk, and Linux naturally has it. After reading the comparison and description of ramdisk, tmpfs, and ramfs in Linux After that, decided to use the tmpfs file system.

 

 

About tmpfs and VMs

linux principle writes
Linux memory (VM), including ram and swap two parts.
  •  Ram is your physical memory, and swap is the swap partition divided when installing the system (called the page file under win, and it is located in C:\pagefiles.sys by default).
  • VM is also the maximum memory limit that a program can use, tmpfs is essentially a VM, and tmpfs draws out some pages of the VM as a file system.

Advantages of tmpfs

  • Because tmpfs is built on a VM, not a physical disk, it can be used without formatting, so don't try mkfs.tmpfs, there is no such command.

  • The size of tmpfs is dynamic, as much as it is used, and deleting files will free up the corresponding VM space. =.= This is very exciting.

  • When your physical memory is not enough to support the size allocated as tmpfs, it will automatically use swap pages.

  • tmpfs resides in physical memory most of the time, which makes it super fast to read and write.

Disadvantages of tmpfs:

 

 

The innate advantages of tmpfs become his innate disadvantages:

  • The characteristics of memory cause data located on tmpfs to be lost when power is lost.
  • Compared with the price of hard disk, memory is undoubtedly very expensive, so don't put too big things in the directory of tmpfs.

 

 

Purpose of tmpfs

  • Temporary files generated by program running, I think tmpfs is designed for tmp directories by nature.
  • The cache directory at compile time is really good to use memory.

Usage of tmpfs

 

mount tmpfs /tmp -t tmpfs -o size=1024m

 

 

  • 1024 is not fixed, and the actual size is determined according to your actual situation. For example, you can see how much (free memory and swap) there is in total when the system is running for a long time and under heavy load. It is best not to exceed this value.
  • There must be mount permission here, you can use sudo to obtain permission, 'size=' specifies the upper limit of the dynamic size of tmpfs, if the size of the /tmp directory (to be used) exceeds the specified size, it will also prompt you that there is insufficient space.
  • The reason why this is not recommended is because /tmp will be emptied immediately after mount. If you have a program that has files (such as sockets) opened in /tmp, there will be errors, and you have to manually mount it every time, which is very troublesome ( =.=#).
    For the above reasons, it is best to edit the /etc/fstab file to let the system do it for you when it starts up.

The recommended method is as follows:

 

sudo gedit /etc/fstab

Add the following at the end:

mount tmpfs in /tmp/
tmpfs /tmp tmpfs size=1024m 0 0

 Save, close, and you'll have tmpfs on the next boot.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326144071&siteId=291194637