Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Linux memory File system tmpfs

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Linux memory file system tmpfs". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the Linux memory file system tmpfs.

1. / dev/shm theory

The default kernel configuration in the Linux distribution turns on tmpfs and maps to the shm directory under / dev/. You can view the results through the df command.

/ dev/shm/ is a very useful directory under linux because it's not on your hard drive, it's in memory. Therefore, under linux, you don't need to go to a lot of trouble to build ramdisk, but you can achieve a good optimization effect by directly using / dev/shm/. The default system loads / dev/shm, which is called tmpfs, which some say is different from ramdisk (virtual disk). Like a virtual disk, tmpfs can use your RAM, but it can also use your swap partition for storage. And the traditional virtual disk is a block device and requires a command like mkfs to actually use it. Tmpfs is a file system, not a block device; just install it and it's ready to use.

Tmpfs has the following advantages:

one. The size of the dynamic file system, one of the things / dev / shm/ needs to pay attention to is the capacity problem. Under linux, it defaults to half the size of memory, which can be seen using the df-h command. But it will not really occupy this piece of memory, if there are no files under / dev/shm/, the memory it takes is actually 0 bytes; if it is up to 1G and contains 100m files, then the remaining 900m can still be used by other applications, but the 100m of memory it occupies will never be reclaimed and redivided by the system.

two. Another major benefit of tmpfs is its lightning speed. Because a typical tmpfs file system resides entirely in RAM, reads and writes can be almost instantaneous.

three. Tmpfs data is not retained after a reboot because virtual memory is inherently volatile. So it is necessary to do some scripts to do operations such as loading and binding.

II. Modify / dev/shm size

The default maximum half of the memory size may not be enough in some cases, and the default amount of inode is generally increased, so you can use the mount command to manage it.

# mount-o size=1500M-o nr_inodes=1000000-o noatime,nodiratime-o remount / dev/shm

On 2G machines, adjust the maximum capacity to 1.5G and the number of inode to 1000000, which means that up to a million small files can be saved roughly.

If you need to permanently modify the value of / dev/shm, you need to modify / etc/fstab

The code is as follows:

Tmpfs / dev/shm tmpfs defaults,size=1.5G 0 0

Mount-o remount / dev/shm

3. / dev/shm application

First create a tmp folder in / dev/shm, and then bind it to the actual / tmp

The code is as follows:

# mkdir / dev/shm/tmp

# chmod 1777 / dev/shm/tmp

# mount-bind / dev/shm/tmp / tmp (- bind)

After you use the mount-bind olderdir newerdir command to mount one directory to another, all information such as the permissions and owners of newerdir changes. The mounted directory inherits all the properties of the mounted directory, except for the name.

At this point, I believe that everyone on the "Linux memory file system tmpfs use" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report