swap空间可以有效缓解内存压力
不太了解底层的人对swap空间的概念也很模糊,这里我简单举例,看看swap空间的作用
查看当前swap空间:3个方式
[root@localhost /home/xxx/kirin/os_diagnosis]
#swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -1
#cat /proc/swaps
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -1
[root@localhost /home/xxx/kirin/os_diagnosis]
#free -m
total used free shared buff/cache available
Mem: 96479 1488 79007 4169 15983 90466
Swap: 2047 0 2047
关闭(释放)swap空间
[root@localhost /home/xxx/kirin/os_diagnosis]
#swapoff /dev/sda3
[root@localhost /home/xxx/kirin/os_diagnosis]
#free -m
total used free shared buff/cache available
Mem: 96479 1487 79009 4169 15982 90468
Swap: 0 0 0
一个吃内存程序:dd
#systemctl cat dd
# /etc/systemd/system/dd.service
[Unit]
Description=dd
ConditionFileIsExecutable=/usr/libexec/cc.py
[Service]
Type=simple
ExecStart=/usr/libexec/dd
Slice=jiangyi.slice
CPUAccounting=yes
CPUQuota=40%
MemoryAccounting=yes
MemoryMax=100M
MemoryLimit=200M
TasksAccounting=yes
BlockIOAccounting=yes
[Install]
WantedBy=multi-user.target
#cat /usr/libexec/dd
#!/usr/bin/bash
x="a"
while [ True ];do
x=$x$x
done;
现象:dd程序立刻OOM,并且dd程序没有重启启动
[847607.021675] Call Trace:
[847607.024362] [<ffffffff81637acc>] dump_stack+0x19/0x1b
[847607.029727] [<ffffffff816329ba>] dump_header+0x8e/0x214
[847607.035264] [<ffffffff8116c796>] ? find_lock_task_mm+0x56/0xc0
[847607.041407] [<ffffffff8116cc2a>] oom_kill_process+0x24a/0x3b0
[847607.047462] [<ffffffff8108341e>] ? has_capability_noaudit+0x1e/0x30
[847607.054038] [<ffffffff811d417f>] mem_cgroup_oom_synchronize+0x50f/0x530
[847607.060955] [<ffffffff811d3420>] ? mem_cgroup_can_attach+0x1b0/0x1b0
[847607.067615] [<ffffffff8116d4a4>] pagefault_out_of_memory+0x14/0x90
[847607.074100] [<ffffffff81630e15>] mm_fault_error+0x8e/0x180
[847607.079915] [<ffffffff81643971>] __do_page_fault+0x3e1/0x420
[847607.085906] [<ffffffff816439d3>] do_page_fault+0x23/0x80
[847607.091535] [<ffffffff8163fcc8>] page_fault+0x28/0x30
[847607.096897] Task in /jiangyi.slice/dd.service killed as a result of limit of /jiangyi.slice/dd.service
[847607.106574] memory: usage 204800kB, limit 204800kB, failcnt 22
[847607.112629] memory+swap: usage 204800kB, limit 9007199254740991kB, failcnt 0
[847607.119907] kmem: usage 0kB, limit 9007199254740991kB, failcnt 0
[847607.126135] Memory cgroup stats for /jiangyi.slice/dd.service: cache:0KB rss:204800KB rss_huge:133120KB mapped_file:0KB swap:0KB inactive_anon:0KB active_anon:204788KB inactive_file:0KB active_file:0KB unevictable:0KB
[847607.146201] [ pid ] uid tgid total_vm rss nr_ptes swapents oom_score_adj name
[847607.154528] [ 5021] 0 5021 110741 51497 115 0 0 dd
[847607.162544] Memory cgroup out of memory: Kill process 5021 (dd) [State: 0 Flags: 4202752] score 977 or sacrifice child
[847607.173585] Killed process 5021 (dd) total-vm:442964kB, anon-rss:204720kB, file-rss:1268kB
Apr 27 14:03:32 localhost systemd[1]: Started dd.
Apr 27 14:03:32 localhost systemd[1]: Starting dd...
Apr 27 14:03:35 localhost systemd[1]: dd.service: main process exited, code=killed, status=9/KILL
Apr 27 14:03:35 localhost systemd[1]: Unit dd.service entered failed state.
Apr 27 14:03:35 localhost systemd[1]: dd.service failed.
开启SWAP空间
[root@localhost /home/xxx/kirin/os_diagnosis]
#swapon /dev/sda3
[root@localhost /home/xxx/kirin/os_diagnosis]
#swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -1
[root@localhost /home/xxx/kirin/os_diagnosis]
#free -m
total used free shared buff/cache available
Mem: 96479 1486 79009 4169 15983 90471
Swap: 2047 0 2047
现象: dd程序没有立刻OOM,而是先用SWAP空间,监控swapin swapout可以看到大量页面置换,重要的是,swap使用空间没有一直增长,也有降低。这就看拆东墙补西墙的速度了,最终,当swap空间被使用完的一瞬间,dd程序再申请内存,触发了pagefault,此时才会触发OOM,这个dd程序将被干掉!

OOM瞬间:swapd使用量跌0,so si bi bo全部跌0

大量swapout swapin页置换
2017-04-27 14:07:42,PAGE_AND_SWAP_LIVE,page_in,39448.00,pages/s
2017-04-27 14:07:42,PAGE_AND_SWAP_LIVE,page_out,58680.00,pages/s
2017-04-27 14:07:42,PAGE_AND_SWAP_LIVE,swap_in,9776.00,pages/s
2017-04-27 14:07:42,PAGE_AND_SWAP_LIVE,swap_out,14646.00,pages/s
思考:OOM是在一个程序无法申请内存地址的时候才会发生,开启swap地址,可以有效缓解内存的使用。
Swap分区空间什么时候使用
系统在什么情况或条件下才会使用Swap分区的空间呢? 其实是Linux通过一个参数swappiness来控制的。当然还涉及到复杂的算法。
这个参数值可为 0-100,控制系统 swap 的使用程度。高数值可优先系统性能,在进程不活跃时主动将其转换出物理内存。低数值可优先互动性并尽量避免将进程转换处物理内存,并降低反应延迟。默认值为 60。
注意:这个只是一个权值,不是一个百分比值,涉及到系统内核复杂的算法。下面是关于swappiness的相关资料
The Linux 2.6 kernel added a new kernel parameter called swappiness to let administrators tweak the way Linux swaps. It is a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle. Kernel maintainer Andrew Morton has said that he runs his desktop machines with a swappiness of 100, stating that "My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful."
Swappiness is a property of the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space. The default value is 60, and for most desktop systems, setting it to 100 may affect the overall performance, whereas setting it lower (even 0) may improve interactivity (by decreasing response latency.
有两种临时修改swappiness参数的方法,系统重启后失效
# echo 10 > /proc/sys/vm/swappiness
#sysctl vm.swappiness=10
永久
echo 'vm.swappiness=10' >>/etc/sysctl.conf
疑问
如果有人会问是否物理内存使用到某个百分比后才会使用Swap交换空间,可以明确的告诉你不是这样一个算法,及时物理内存只剩下8M了,但是依然没有使用Swap交换空间,而另外一个例子,物理内存还剩下19G,居然用了一点点Swap交换空间。
另外调整/proc/sys/vm/swappiness这个参数,如果你没有绝对把握,就不要随便调整这个内核参数,这个参数符合大多数情况下的一个最优值。
Swap分区大小设置
ORACLE的官方文档就推荐如下设置,这个是根据物理内存来做参考的

在其它博客中看到下面一个推荐设置,当然我不清楚其怎么得到这个标准的。是否合理也无从考证。可以作为一个参考。
4G以内的物理内存,SWAP 设置为内存的2倍。
4-8G的物理内存,SWAP 等于内存大小。
8-64G 的物理内存,SWAP 设置为8G。
64-256G物理内存,SWAP 设置为16G。
SWAP的优点和缺点:
优点:
- Provides overflow space when your memory fills up completely
- Can move rarely-needed items away from your high-speed memory
- Allows you to hibernate
缺点:
- Takes up space on your hard drive as SWAP partitions do not resize dynamically
- Can increase wear and tear to your hard drive
- Does not necessarily improve performance (see below)
swap占用的是磁盘的空间,如果,内存充足,压根没有用上swap空间,变相的相当于浪费了磁盘空间;swap空间大小不可以动态调整
REF
http://www.cnblogs.com/kerrycode/p/5246383.html
swap空间可以有效缓解内存压力的更多相关文章
- SWPFILE实现(增加swap空间)
1.mkdir /var/swap chmod 700 /var/swap(可以不用设置) 2.dd if=/dev/zero of=/var/swap/file bs=1024 count=65 ...
- 手动增加swap空间
在日常工作中,swap没有必要搞那么大的空间,因为现在好多服务器都使用了ssd硬盘,这些硬盘还是比较贵的.如果服务器内存是128G,swap空间还设置成内存的两倍的话,那岂不是很明显是很 ...
- linux增加swap空间的方法小结
起因及背景 近期编译AOSP(android 10.0)是总是遇到内存溢出,查了半天,无果.猜测增加下swap空间大小是否能解决,随即尝试下,果然是如此. 当然,还有其他作法,比如直接增加主机的内存( ...
- Linux 磁盘挂载和swap空间管理
挂载:把指定的设备和根下面的某个文件夹建立关联 卸载:解除两者关系的过程 挂载文件系统:mount 格式:mount device mountpoint --- mount 设备名 挂载点 mount ...
- Sql Server 内存相关计数器以及内存压力诊断
在数据库服务器中,内存是数据库对外提供服务最重要的资源之一, 不仅仅是Sql Server,包括其他数据库,比如Oracle,MySQL等,都是一类非常喜欢内存的应用. 在Sql Server服务器中 ...
- Performance Monitor3:监控SQL Server的内存压力
SQL Server 使用的资源受到操作系统的调度,同时,SQL Server在内部实现了一套调度算法,用于管理从操作系统获取的资源,主要是对内存和CPU资源的调度.一个好的数据库系统,必定在内存中缓 ...
- Linux学习(十四)磁盘格式化、磁盘挂载、手动增加swap空间
一.磁盘格式化 分好去的磁盘需要格式化之后才可以使用.磁盘分区一般用mke2fs命令或者mkfs.filesystemtype.这个filesystemtype分为ext4,ext3,xfs等等.xf ...
- linux swap空间的swappiness=0
linux 会使用硬盘的一部分做为SWAP分区,用来进行进程调度--进程是正在运行的程序--把当前不用的进程调成‘等待(standby)‘,甚至‘睡眠(sleep)’,一旦要用,再调成‘活动(acti ...
- 问题:Linux报swap空间占用过高,但物理内存还有空余
报错 收到报警,swap空间占用过高,登录到系统查看内存使用详情,看到物理内存还有很多未使用 问题分析 Swap配置对性能的影响分配太多的Swap空间会浪费磁盘空间,而Swap空间太少,则系统会发生错 ...
随机推荐
- HDU 1704 Rank
Rank Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 17046 ...
- js setTimeout函数
最近在看JS DOM编程艺术,在第十章的动画里面有个setTimeout函数的例子中涉及了很多的引号,研究了好大一会才看明白,综合网上各个大神的解释和自己的理解,其原理是这样的: 首先看下程序源代码: ...
- ZOJ 1450
最小圆覆盖 #include <iostream> #include <algorithm> #include <cstdio> #include <cmat ...
- php模拟并发
原文: http://blog.csdn.net/zhang_xinglong/article/details/16339867 ----------------------------------- ...
- Ural 1167 Bicolored Horses (DP)
题目地址:Ural 1167 感觉这题的思路类似于背包的做法. . 先预处理出来每一个马与之前全部的马的0的数量和1的数量,用数组a[0][i]和a[1][i]来表示. 然后再用数组dp[i][j]来 ...
- 我的红外arduino链接,!!!!
点击打开链接http://blog.csdn.net/g1342522389/article/details/46272473 一定要赞,小编非常辛苦.
- Fitnesse Page 简单使用
more information- http://www.fitnesse.org/FitNesse.UserGuide 1.1 Edit 点击该按钮,则可以开始编辑(如果该按钮没有出现,则这个页 ...
- spring属性的三种注入方法
(1)使用set方法: public class Book { private String bookname;public void setBookname(String bookname) { ...
- Swift_ios_二进制,十进制,十六进制之间的转换
这里所说的转换,并不是Swift中字面量之间的转换.如果是字面量之间的转换,ios系统中已经自动帮我们转换了. 例如let number1:Int = 8let number2:Int = 0b100 ...
- [.Net] Excel导入导出各种方式分析
1.引言 1.1解决哪些问题 现在很多公司用的导出基本上采用的通过gridView导出excel,此种导出存在以下几种问题 1.数据量大的时候有时导出有时会让浏览器卡死,因为导出的excel不是真 ...