Linux 查询内存的命令 free -h 中Swap与Memory的区别

 

Swap与Memory区别

对于memory没什么可说的就是机器的物理内存,读写速度低于cpu一个量级,但是高于磁盘不止一个量级。所以,程序和数据如果在内存的话,会有非常快的读写速度。但是,内存的造价是要高于磁盘的,虽然相对来说价格一直在降低。除此之外,内存的断电丢失数据也是一个原因说不能把所有数据和程序都保存在内存中。既然不能全部使用内存,那数据还有程序肯定不可能一直霸占在内存中。

当内存没有可用的,就必须要把内存中不经常运行的程序给踢出去。但是:踢到哪里去?这时候swap就出现了。swap全称为swap place,即交换区。当内存不够的时候,被踢出的进程被暂时存储到交换区。当需要这条被踢出的进程的时候,就从交换区重新加载到内存,否则它不会主动交换到真实内存中。

 

一、Swap介绍

在详细介绍swap之前,我们需要知道的是计算机对内存分为物理内存与虚拟内存(注意虚拟内存和虚拟地址空间的区别)。物理内存就是计算机的实际内存大小,由RAM芯片组成的。虚拟内存则是虚拟出来的、使用磁盘代替内存。虚拟内存的出现,让机器内存不够的情况得到部分解决。当程序运行起来由操作系统做具体虚拟内存到物理内存的替换和加载(相应的页与段的虚拟内存管理)。这里的虚拟内存即所谓的swap。
当用户提交程序,然后产生进程,在机器上运行。机器会判断当前物理内存是否还有空闲允许进程调入内存运行,如果有那么则直接调入内存进行运行;如果没有,那么会根据优先级选择一个进程挂起,把该进程交换到swap中等待,然后把新的进程调入到内存中运行。根据这种换入和换出,实现了内存的循环利用,让用户感觉不到内存的限制。从这也可以看出swap扮演了一个非常重要的角色,就是暂存被换出的进程。 内存与swap之间是按照内存页为单位来交换数据的,一般Linux中页的大小设置为4kb。而内存与磁盘则是按照块来交换数据的。

 

二、Swap的设置

从上可以看出,当物理内存使用完或者达到一定比例之后,我们可以使用swap做临时的内存使用。当物理内存和swap都被使用完那么就会出错:“out of memory(内存不足)”。对于使用多大比例内存之后开始使用swap,在系统的配置文件中可以通过调整参数进行修改。

cat /proc/sys/vm/swappiness 60

该参数可以从0-100进行设置。0就是最大限度使用内存,尽量不使用swap;100就是积极使用swap。这个具体的通过系统的算法进行确定。

 

物理内存我们是无法更改的,所以swap的大小设置将会影响应用能否正常运行。

那么:swap大小如何确定?

根据centos官网介绍可以得出如下公式:

M = Amount of RAM in GB, and S = Amount of swap in GB, then If M < 2, S = M *2 Else S = M + 2。而且其最小不应该小于32M(never less than 32 MB.)。

 

swap分区的数量对性能也有很大的影响。因为swap毕竟还是以磁盘来伪装成内存,交换的操作是磁盘IO的操作而不是内存的load与store操作。如果有多个swap交换区,每个swap会有一定的优先级,该优先级也可以调整。swap空间的分配会以轮流的方式操作于所有的swap,这样会大大均衡IO的负载,加快swap交换的速度。

 

三、Swap相关命令

swapon/swapoff swap-disk_name:启动和关闭相应的swap_disk_name

swapon -s :可以查看当期swap的使用情况,也可以通过 cat /proc/swaps命令查看

### Linux `free -h` Command Usage and Explanation The `free` command provides a snapshot of the system's memory usage, including both RAM and swap space. When using `-h`, it displays all values in human-readable format (Kilobytes, Megabytes, Gigabytes). #### Syntax To use this command with human-readable output: ```bash free -h ``` This will display information about total, used, and available memory along with buffers/cache details. #### Output Columns Explained - **total**: Total installed physical or swap memory. - **used**: Memory currently being utilized by active processes. - **free**: Unused memory that is completely free to allocate immediately without needing any page eviction or swapping activity. - **shared**: Memory used by tmpfs (since version 3.18). - **buff/cache**: Sum of buffers and cache which can be quickly reclaimed when more memory is needed. - **available**: Estimation of how much memory is available for starting new applications without swapping out others; calculated from MemFree, Buffers, Cached, SReclaimable etc[^1]. For instance, running `free -h` might produce an output similar to below: ```plaintext total used free shared buff/cache available Mem: 7.7G 2.0G 4.9G 156M 804M 5.2G Swap: 2.0G 0B 2.0G ``` In this example, there are approximately 7.7 gigabytes of total RAM on the machine where around 2 gigabytes have been allocated while nearly 5 gigabytes remain unused but may still contain cached data ready for rapid reuse. --related questions-- 1. What does each column represent in the output of the `free -h` command? 2. How do buffer caches affect the reported amount of usable memory according to `free -h`'s results? 3. Can you explain what happens if my application requires more memory than shown under 'available'? 4. Is it normal for swap usage to increase even though plenty of RAM appears unallocated based on `free -h`? 5. Why would someone prefer checking memory status via `top` over simply using `free -h`?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴国进

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值