【Linux】What Is dev shm And Its Practical Usage

简介: 【Linux】What Is dev shm And Its Practical Usage

原文

What Is /dev/shm And Its Practical Usage - nixCraft (cyberciti.biz)

介绍

/dev/shm is nothing but implementation of traditional shared memory concept.

/dev/shm只不过是传统共享内存概念的实现。

It is an efficient means of passing data between programs.

它是一种在程序间传递数据的有效方法。

One program will create a memory portion, which other processes (if permitted) can access.

一个程序将创建一个内存部分,其他进程(如果允许)可以访问该部分。

This will result into speeding up things on Linux.

这将(意味着)加快 Linux 的运行速度。

shm / shmfs is also known as tmpfs, which is a common name for a temporary file storage facility on many Unix-like operating systems.

shm / shmfs 也称为 tmpfs,是许多类 Unix 操作系统中临时文件存储设备的通用名称。

It is intended to appear as a mounted file system, but one which uses virtual memory instead of a persistent storage device.

tmpfs 的内容是显示已加载的文件系统、但是使用的是用户虚拟内存而不是一个永久存储设备

If you type the mount command you will see /dev/shm as a tempfs file system.

如果键入挂载命令,就会看到 /dev/shm 作为 tempfs 文件系统。

Therefore, it is a file system, which keeps all files in virtual memory.

因此,(/dev/shm它是一个将所有文件保存在虚拟内存中的文件系统

Everything in tmpfs is temporary in the sense that no files will be created on your hard drive.

注意,所有在tmpfs中发生的事情都是临时的,不会在硬盘上创建任何文件。

If you unmount a tmpfs instance, everything stored therein is lost.

如果你卸载一个tmpfs 实例,它内部的所有存储内容都将丢失。

By default almost all Linux distros configured to use /dev/shm:

默认情况下,几乎所有 Linux 发行版都配置使用 /dev/shm

比如你使用下面的命令。


$ df -h

Sample outputs:

输出示例


Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/wks01-root
                      444G   70G  351G  17% /
tmpfs                 3.9G     0  3.9G   0% /lib/init/rw
udev                  3.9G  332K  3.9G   1% /dev
tmpfs                 3.9G  168K  3.9G   1% /dev/shm
/dev/sda1             228M   32M  184M  15% /boot

Nevertheless, where can I use /dev/shm? 但是,在哪里可以使用 /dev/shm?

You can use /dev/shm to improve the performance of application software such as Oracle or overall Linux system performance.

您可以使用 /dev/shm 来提高 Oracle 等应用软件的性能或 Linux 系统的整体性能。

On heavily loaded system, it can make tons of difference.

在负载较重的系统中,它可以产生巨大的差异。

For example VMware workstation/server can be optimized to improve your Linux host’s performance (i.e. improve the performance of your virtual machines).

例如,可以对 VMware 工作站/服务器进行优化,以提高 Linux 主机的性能(即提高虚拟机的性能)。

In this example, remount /dev/shm with 8G size as follows:

比如下面这个例子,重新挂载 8G 大小的 /dev/shm,如下所示:


mount -o remount,size=8G /dev/shm

To be frank, if you have more than 2GB RAM + multiple Virtual machines, this hack always improves performance.

坦率地说,如果你有超过 2GB 内存和多个虚拟机,这个黑客程序总能提高性能。

In this example, you will give you tmpfs instance on /disk2/tmpfs which can allocate 5GB RAM/SWAP in 5K inodes and it is only accessible by root:

在本例中,你将在 /disk2/tmpfs 上创建一个 tmpfs 实例,它可以在 5K inodes 中分配 5GB RAM/SWAP,并且只能由 root 访问:


mount -t tmpfs -o size=5G,nr_inodes=5k,mode=700 tmpfs /disk2/tmpfs

Where,

命令解释:

  • -o opt1,opt2 : Pass various options with a -o flag followed by a comma separated string of options. In this examples, I used the following options:
  • -o opt1,opt2 : 使用 -o 标志传递各种选项,后跟逗号分隔的选项字符串。在本例中,我使用了以下选项:
  • remount : Attempt to remount an already-mounted filesystem. In this example, remount the system and increase its size.
  • remount : 尝试重新挂载已挂载的文件系统。在此示例中,重新挂载系统并增大其大小。
  • size=8G or size=5G : Override default maximum size of the /dev/shm filesystem. he size is given in bytes, and rounded up to entire pages. 覆盖 /dev/shm 文件系统的默认最大大小。大小以字节为单位,四舍五入为整页。
    The default is half of the memory. 默认值为内存的一半。
    The size parameter also accepts a suffix % to limit this tmpfs instance to that percentage of your pysical RAM: the default, when neither size nor nr_blocks is specified, is size=50%. size 参数也接受后缀 %,用于将此 tmpfs 实例限制为实际内存的该百分比:当既未指定 size 也未指定 nr_blocks 时,默认值为 size=50%
    In this example it is set to 8GiB or 5GiB. The tmpfs mount options for sizing ( size, nr_blocks, and nr_inodes) accept a suffix k, m or g for Ki, Mi, Gi (binary kilo, mega and giga) and can be changed on remount. 在本例中,它被设置为 8GiB 或 5GiB。tmpfs 挂载选项的大小(size、nr_block 和 nr_inodes)接受 Ki、Mi、Gi(二进制千、兆和千兆)的后缀 k、m 或 g,并可在重新挂载时更改。
  • nr_inodes=5k :
  • The maximum number of inodes for this instance. The default is half of the number of your physical RAM pages, or (on a machine with highmem) the number of lowmem RAM pages, whichever is the lower. 此实例的最大 inodes 数量。默认值是物理 RAM 页数的一半,或(在使用高内存的机器上)低内存 RAM 页数的一半,以较低者为准。
  • mode=700 : Set initial permissions of the root directory. 设置根目录的初始权限。
  • tmpfs : Tmpfs is a file system which keeps all files in virtual memory. Tmpfs 是一种将所有文件保存在虚拟内存中的文件系统。

How do I restrict or modify size of /dev/shm permanently?如何永久限制或修改 /dev/shm 的大小?

You need to add or modify entry in /etc/fstab file so that system can read it after the reboot. Edit, /etc/fstab as a root user, enter:

您需要添加或修改 /etc/fstab 文件中的配置,以便系统能在重启后读取它。以根用户身份编辑 /etc/fstab,输入


vi /etc/fstab

Append or modify /dev/shm entry as follows to set size to 8G

添加或修改 /dev/shm 条目如下,将大小设置为 8G


none      /dev/shm        tmpfs   defaults,size=8G        0 0

Save and close the file. For the changes to take effect immediately remount /dev/shm:

保存并关闭文件。要使更改立即生效,请重新挂载 /dev/shm


mount -o remount /dev/shm

Verify the same:

验证是否一致:


df -h

Recommend readings:

推荐读物:


相关文章
|
4月前
|
Unix Linux Shell
linux中nohup和/dev/null,21
linux中nohup和/dev/null,21
104 0
|
4月前
|
Linux Windows
Linux中/dev/null和/dev/zero的作用
Linux中/dev/null和/dev/zero的作用
79 2
|
4月前
|
存储 关系型数据库 MySQL
Linux的dev/vda1文件满了导致MySQL无法写入
今天查看两个月前上线的小项目,发现运行非常慢,而且增删改查失效了(吓我一大跳),急急忙忙的就开始了我的线上问题排查之路。/dev/vda1是 Linux 系统中的一个设备文件,它表示第一个虚拟磁盘(vda)的第一个分区(1)。在大多数 Linux 发行版中,这是系统根分区的默认位置。如果您要对/dev/vda1进行操作(例如挂载、格式化、备份等),则需要具有足够的权限。一般来说,只有 root 用户或具有 sudo 权限的用户才能进行这些操作。当Linux的/dev/vda1。
425 0
|
Linux
Linux:nohup、&、 2>&1、/dev/null
Linux:nohup、&、 2>&1、/dev/null
943 0
|
29天前
|
监控 Linux
在Linux中,如何检测并修复/dev/hda5?
在Linux中,如何检测并修复/dev/hda5?
|
11月前
|
Linux
百度搜索:蓝易云【Linux系统的 /dev/null 是什么?有什么隐藏的作用?】
综上所述,`/dev/null`在Linux系统中起到丢弃输入并模拟黑洞的作用。通过将输出重定向到 `/dev/null`,可以隐藏输出、屏蔽错误信息或作为空输出的占位符。它是一种非常有用的工具,特别是在自动化脚本和命令行操作中。
97 2
|
4月前
|
安全 Unix Linux
探秘Linux特殊设备文件:(/dev/null,/dev/zero,/dev/random,/dev/urandom等)
探秘Linux特殊设备文件:(/dev/null,/dev/zero,/dev/random,/dev/urandom等)
363 1
|
4月前
|
安全 Linux 数据安全/隐私保护
Linux中/dev/random和/dev/urandom的作用
Linux中/dev/random和/dev/urandom的作用
93 0
|
Linux
【Linux】关于Bad magic number in super-block 当尝试打开/dev/sda1 时找不到有效的文件系统超级块
【Linux】关于Bad magic number in super-block 当尝试打开/dev/sda1 时找不到有效的文件系统超级块
257 0
|
算法 安全 Linux
Linux 中的 /dev/random 和 /dev/urandom 是什么?
Linux 中的 /dev/random 和 /dev/urandom 是什么?
229 0