Home>Article>Operation and Maintenance> What does linux physical memory mean?
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.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
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 memoryis 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.
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.
##1 Check the system memory free# The
##freecommand can display the unused and used memory size of the current system, and can also display the memory buffer used by the kernel. Enterfree
in the terminal (the parameters will be explained later) to see the memory status of our server, as follows:
: Memory usage informationSwap
: Swap space usage information
: 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.
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
is memory that is not being usedavailable
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 partbuffer/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.
) The display unit is Mb, as shown below:
You can view the detailed command of free throughfree --help
:
You can automatically match units suitable for people's reading habits through free -h, where h means
.
ps:Line 3swap
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.
-b:以批处理模式操作; -c:显示完整的治命令; -d:屏幕刷新间隔时间; -I:忽略失效过程; -s:保密模式; -S:累积模式; -i<时间>:设置间隔时间; -u<用户名>:指定用户名; -p<进程号>:指定进程; -n<次数>:循环显示的次数。
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文件中。
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
查看系统的所有进程的快照。
命令:cat /proc/meminfo
注:initrd和初始化代码init在引导完成之后会被释放掉,所以最终的内核可管理内存(total)会比dmesg显示的available更多一点,相应的源代码可参见: free_initrd_mem() 和 free_initmem()。
优化可以主要从优化保留内存和优化used内存两个方面进行,具体的需要结合代码。
查看磁盘命令比较多,最常用的为:df -lh
命令
命令:lsmod
相关推荐:《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!