Home >Operation and Maintenance >Linux Operation and Maintenance >What does linux physical memory mean?

What does linux physical memory mean?

青灯夜游
青灯夜游Original
2023-03-02 10:30:072645browse

In Linux, physical memory refers to the memory size provided by the system hardware, which is real memory. Linux's memory management adopts a paging access mechanism. In order to ensure that physical memory can be fully utilized, the kernel will automatically swap infrequently used data blocks in physical memory into virtual memory at the appropriate time, and frequently used data blocks will be automatically swapped into virtual memory at the appropriate time. Information is retained to physical memory.

What does linux physical memory mean?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

Virtual memory and physical memory in Linux


We all know that reading and writing data directly from memory is much faster than reading and writing data from the hard disk. Therefore, it is preferable that all data reading and writing are completed in memory. However, memory is limited, which leads to the concepts of physical memory and virtual memory.

Physical memory is the memory size provided by the system hardware, which is real memory. Compared with physical memory, there is also the concept of virtual memory under Linux. Virtual memory is a strategy proposed to meet the shortage of physical memory. It is a piece of logical memory virtualized by using disk space. The disk space used as virtual memory is called swap space (also known as swap space).

As an extension of physical memory, Linux will use the virtual memory of the swap partition when the physical memory is insufficient. More specifically, the kernel will write temporarily unused memory block information to the swap space. In this way , the physical memory is released, and this memory can be used for other purposes. When the original content is needed, the information will be re-read from the swap space into the physical memory.

Linux’s memory management adopts a paging access mechanism. In order to ensure that physical memory can be fully utilized, the kernel will automatically swap infrequently used data blocks in physical memory into virtual memory at appropriate times. , while retaining frequently used information to physical memory.

To have a deep understanding of the Linux memory operating mechanism, you need to know the following aspects:

  • First of all, the Linux system will perform page swap operations from time to time to maintain As much free physical memory as possible, even if nothing needs the memory, Linux will swap out temporarily unused memory pages, because this can greatly save the time required to wait for swapping.

  • Secondly, Linux's page swapping is conditional. Not all pages are swapped to virtual memory when not in use. The Linux kernel only swaps some pages that are not used based on the "most recently used" algorithm. Frequently used page files are swapped into virtual memory.

Sometimes we will see such a phenomenon. Linux still has a lot of physical memory, but a lot of swap space is also used. In fact, this is not surprising. For example, when a process that occupies a lot of memory resources consumes a lot of memory resources when it is running, some infrequently used page files will be swapped into virtual memory, but later the process that occupies a lot of memory resources ends and a lot of memory is released. At this time, the page file that was just swapped out will not be automatically swapped into the physical memory (unless it is necessary), then the system physical memory will be much free at this time, and the swap space is also being used, and the problem just mentioned appears. phenomenon.

Finally, the pages in the swap space will first be swapped to physical memory when used. If there is not enough physical memory to accommodate these pages at this time, they will be swapped out immediately. In this way, the virtual memory There may not be enough space in the system to store these swap pages, which will eventually lead to Linux crashes, service abnormalities and other problems. Although Linux can restore itself within a period of time, the restored system is basically unusable.

Therefore, it is very important to reasonably plan and design the use of Linux memory. Regarding the size settings of physical memory and swap space, it depends on the actual hard disk size used, but generally follows this basic principle:

  • If the memory is small (according to experience, the physical memory is less than 4GB), generally set the swap partition size to 2 times the memory;

  • If the physical memory If the memory size is greater than 4GB and less than 16GB, you can set the swap partition size equal to the physical memory;

  • If the memory size is more than 16GB, you can set swap to 0, but this is not recommended because the setting A swap partition of a certain size has a certain effect.

Linux system to view memory usage


In Windows and Linux operating systems with GUI, we can usually use the UI to view System memory and space usage, but for development or operation and maintenance personnel, who often have to work on Linux servers without GUI, the command line can provide more functions and flexibility than the GUI.

Especially when an application in our system is abnormal, or the system occupancy is abnormal, or Linux development requires memory trimming, we need to understand the system memory and space usage, and we need to master several commonly used View tools.

Common commands

  • View memory usage: free
  • Display process information (including CPU, memory usage, etc.): top, ps
  • Check the memory occupied by the driver: lsmod

##1 Check the system memory free# The

##free

command can display the unused and used memory size of the current system, and can also display the memory buffer used by the kernel. Enter free in the terminal (the parameters will be explained later) to see the memory status of our server, as follows:
What does linux physical memory mean?

1.1 Details The description is as follows:

Mem

: Memory usage information Swap
: Swap space usage information

total

: Total physical memory size. used
: Physical memory has been used. free
: Available physical memory. shared
: The total amount of memory shared by multiple processes. buffers/cached
: The cache buffer uses physical memory size. available
: The size of physical memory that can also be used by the application.

1.2 Relationship between physical memories

total = used free buffer/cache

avaiable = free buffer/cache
(Generally speaking, this is true for personal computers, but if ordinary users of servers or public clouds have some buffers/cache that cannot be used, they will have available

1.3 The difference between free and available

free

is memory that is not being used available
is the memory that the application considers available. In order to improve the read and write performance, Linux consumes a part of the memory resources as cache or buffer memory. From the perspective of the kernel, this part buffer/cache
belongs to the used memory. Memory; when the application applies for memory and the free memory is not enough, the kernel will recycle the buffer and cache to meet the application's memory needs.

1.4 The difference between buffer and cache There is a certain difference between buffers and cache:

A buffer is something that has yet to be “written” to disk. -buffer write cache, when data is stored, it is first saved to the disk buffer and then written to the permanent space
  • A cache is something that has been “ reed" from the disk adn stored for later use. --cache read cache. After the data is read from the disk, it is temporarily stored in the buffer to prepare for subsequent use by the program.

1.5 free parameter descriptionThe unit displayed under the free command is k, you can add -m after free (i.e.

free -m

) The display unit is Mb, as shown below:
You can view the detailed command of free through What does linux physical memory mean?free --help
:
What does linux physical memory mean?You can automatically match units suitable for people's reading habits through free -h, where h means

humanity

.

ps:

Line 3 swap
is the swap partition, which is similar to the virtual memory in the Windows system. When the memory is insufficient, a part of the hard disk space is virtualized into memory usage, thereby solving the problem of insufficient memory capacity.

2 View the memory occupied by the process

The top command can dynamically view the overall operation of the system in real time , is a practical tool that integrates multi-party information monitoring system performance and operation information. The top command can effectively discover system defects, such as insufficient memory, insufficient CPU processing power, and excessive IO reading and writing. The interactive interface provided by the top command can be managed using hotkeys.
    Related syntax:
  • top -X
    -b:以批处理模式操作;
    -c:显示完整的治命令;
    -d:屏幕刷新间隔时间;
    -I:忽略失效过程;
    -s:保密模式;
    -S:累积模式;
    -i<时间>:设置间隔时间;
    -u<用户名>:指定用户名;
    -p<进程号>:指定进程;
    -n<次数>:循环显示的次数。
Some interactive commands that can be used during the execution of the top command. These commands are single-letter, and some of them may be blocked if the -s option is used on the command line. The interactive command is as follows:
  • h:显示帮助画面,给出一些简短的命令总结说明;
    k:终止一个进程;
    i:忽略闲置和僵死进程,这是一个开关式命令;
    q:退出程序;
    r:重新安排一个进程的优先级别;
    S:切换到累计模式;
    s:改变两次刷新之间的延迟时间(单位为s),如果有小数,就换算成ms。输入0值则系统将不断刷新,默认值是5s;
    f或者F:从当前显示中添加或者删除项目;
    o或者O:改变显示项目的顺序;
    l:切换显示平均负载和启动时间信息;
    m:切换显示内存信息;
    t:切换显示进程和CPU状态信息;
    c:切换显示命令名称和完整命令行;
    M:根据驻留内存大小进行排序;
    P:根据CPU使用百分比大小进行排序;
    T:根据时间/累计时间进行排序;
    w:将当前设置写入~/.toprc文件中。
    Directly enter the top command in the terminal, you can see the following interface

2.1 top命令的第一到第五行的详细说明如下:

top - 10:14:31 当前系统时间
up 3 days, 22:36 系统已经运行了3天22h36min
1 users 共有1个用户为登录状态
load average: 0.57, 0.74, 0.65 系统负载,即任务队列的平均长度,load average后面的三个数字分别表示距离现在一分钟,五分钟,十五分钟的负载情况。
注意:load average数据是每隔5秒钟检查一次活跃的进程数,然后按特定算法计算出的数值。如果这个数除以逻辑CPU的数量,结果高于5的时候就表明系统在超负荷运转了。

Tasks: 322 total 总进程数
2 running 正在运行的进程数
320 sleeping 睡眠的进程数
0 stopped 停止的进程数
0 zombie 冻结进程数
%Cpu(s): 2.7 us, 用户空间占用CPU百分比(用户态使用CPU占比)
2.7 sy 内核空间占用CPU百分比 (系统态使用CPU占比)
0.0 ni 用做nice加权的进程分配的用户态cpu时间比
94.0 id 空闲的cpu时间比
0.0 wa IO wait ,cpu等待磁盘写入完成时间
0.0 hi Hardware IRQ,硬中断消耗时间
0.0 si Software IRQ,软中断消耗时间
0.7 st 被hypervisor(管理程序,一般为服务器或者虚拟机)偷取时间
MiB Mem : 11995.2 total 物理内存总量,单位:Mb
360.9 free 空闲内存总量
6766.0 used 使用的物理内存总量,此处需要注意的是:used实际指的是现在系统内核控制的内存数,空闲内存总量(free)是内核还未纳入其管控范围的数量。纳入内核管理的内存不见得都在使用中,还包括过去使用过的现在可以被重复利用的内存,内核并不把这些可被重新使用的内存交还到free中去,因此在linux上free内存会越来越少,但不用为此担心。
4868.3+buff/cache 用作内核缓存的内存量
MiB Swap: 7680.0 total 交换区总量
7433.1 free 空闲交换区总量
246.9 used 使用的交换区总量
3665.4 avail Mem 在不交换的情况下,对启动新应用程序可用内存的估计
(网上也有说法是交换区的可用容量)
top命令第六行为空。
top命令第七行是各个进程的监控:

从左到右依次为:

PID — 进程id
USER — 进程所有者
PR — 进程优先级
NI — nice值。负值表示高优先级,正值表示低优先级
VIRT — 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
RES — 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
SHR — 共享内存大小,单位kb
S — 进程状态。D=不可中断的睡眠状态 R=运行 S=睡眠 T=跟踪/停止 Z=僵尸进程
%CPU — 上次更新到现在的CPU时间占用百分比
%MEM — 进程使用的物理内存百分比
TIME+ — 进程使用的CPU时间总计,单位1/100秒
COMMAND — 进程名称(命令名/命令行)

需要注意的是,此界面显示的并不是所有进程,由于页面显示限制仅仅显示了这几行,我们可以通过top -b -n 1查看系统的所有进程的快照。

3查看内核占用内存

命令:cat /proc/meminfo
What does linux physical memory mean?

注:initrd和初始化代码init在引导完成之后会被释放掉,所以最终的内核可管理内存(total)会比dmesg显示的available更多一点,相应的源代码可参见: free_initrd_mem() 和 free_initmem()。
优化可以主要从优化保留内存和优化used内存两个方面进行,具体的需要结合代码。
查看磁盘命令比较多,最常用的为: df -lh 命令

4 lsmod查看驱动占用内存

命令:lsmod
What does linux physical memory mean?

  • 功能:
    列出已加载的模块,以友好的方式显示/proc/modules的内容
  • 格式:
    第一列:Module表示模块的名称,如sysDebug
    第二列:Size表示模块大小,单位:byte
    第三列:Used 表示依赖的模块个数
    第四列:by表示依赖的模块内容
  • 示例:
    lsmod|grep -i ext3 //查看当前系统是否加载ext3驱动模块

相关推荐:《Linux视频教程

The above is the detailed content of What does linux physical memory mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn