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空间太少,则系统会发生错 ...
随机推荐
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
本文介绍在 Spring Boot 中如何使用定时任务,使用非常简单,就不做过多说明了. com.kfit.base.scheduling.SchedulingConfig: package com. ...
- Elasticsearch顶尖高手系列课程推荐
Elasticsearch,是目前行业中非常热门的一个技术.Elasticsearch是一种分布式的海量数据搜索与分析的技术,可以用于电商网站.门户网站.企业IT系统等各种场景下的搜索引擎,也可以用于 ...
- 错误总结之播放器(vitamio)音量实体键与触摸手势控制,音量调节冲突
这个但是独家心得:经过几天的网上资料查询未果,在群里遇到一同行. 然后让他帮我看了看,终于攻克了该冲突. 此时,谨以此来感谢那位同僚的热情帮助: 说说这个问题吧: 眼下我在做一款影视方面的项目,在该项 ...
- android小技巧:在activity中实现与绑定的fragment的回调
看到标题你可能会想是一个多么高大上的技巧呢?事实上非常一般就是自己定义回调函数. 首先我们知道activity之间的数据传递有几种方式: 一是startActivityForResut()启动一个ac ...
- er图简单回顾
实体对象:矩形 属性:椭圆 关系:菱形 一对一,一对多,多对一,多对多
- fragment.setMenuVisibility setUserVisibleHint
[Android]Fragment真正意义上的onResume和onPause 前言 Fragment虽然有onResume和onPause的,但是这两个方法是Activity的方法,调用时机也是与A ...
- 0x5C 数位统计DP
怎么说,数位DP还是我的噩梦啊,细节太恐怖了. 但是这章感觉又和之前的学的数位DP有差异?(应该是用DP预处理降低时间复杂度,好劲啊,不过以前都是记忆化搜索的应该不会差多少) poj3208 f[i] ...
- Configure environment variables for different tools in jenkins
安装以下的工具,并在Jenkins中的Manage Jenkins-->Configure System-->Global Properties-->Environment Vari ...
- C语言 - 头文件使用案例
源代码分门别类管理,通过头文件. 放置一些函数声明,变量声明,常量定义,宏定义. hotel.h #ifndef HOTEL_H_INCLUDED #define HOTEL_H_INCLUDED # ...
- 杂项:DS(目录服务)
ylbtech-杂项:DS(目录服务) 1.返回顶部 1. DS(目录服务). 目录服务管理概述: 目录服务是扩展计算机系统中最重要的组件之一.虽然用户和管理通常不知道他们感兴趣对象的确切名称,但他们 ...