添加交换分区

SWAP(交换)分区是一种通过在硬盘中预先划分一定的空间,然后将把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理内存不足的问题。但由于交换分区毕竟是通过硬盘设备读写数据的,速度肯定要比物理内存慢,所以只有当真实的物理内存耗尽后才会调用交换分区的资源。

交换分区的创建过程与前文讲到的挂载并使用存储设备的过程非常相似。在对/dev/sdb存储设备进行分区操作前,有必要先说一下交换分区的划分建议:在生产环境中,交换分区的大小一般为真实物理内存的1.5~2倍,为了让大家更明显地感受交换分区空间的变化,这里取出一个大小为5GB的主分区作为交换分区资源。在分区创建完毕后保存并退出即可:

第一步:划分5G大小的磁盘

  1. [root@ken ~]# fdisk /dev/sdb
  2. Welcome to fdisk (util-linux 2.23.2).
  3.  
  4. Changes will remain in memory only, until you decide to write them.
  5. Be careful before using the write command.
  6.  
  7. Command (m for help): n
  8. Partition type:
  9. p primary (1 primary, 0 extended, 3 free)
  10. e extended
  11. Select (default p):
  12. Using default response p
  13. Partition number (2-4, default 2):
  14. First sector (206848-41943039, default 206848):
  15. Using default value 206848
  16. Last sector, +sectors or +size{K,M,G} (206848-41943039, default 41943039): +5G
  17. Partition 2 of type Linux and of size 5 GiB is set
  18.  
  19. Command (m for help): P
  20.  
  21. Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
  22. Units = sectors of 1 * 512 = 512 bytes
  23. Sector size (logical/physical): 512 bytes / 512 bytes
  24. I/O size (minimum/optimal): 512 bytes / 512 bytes
  25. Disk label type: dos
  26. Disk identifier: 0x52997d99
  27.  
  28. Device Boot Start End Blocks Id System
  29. /dev/sdb1 2048 206847 102400 83 Linux
  30. /dev/sdb2 206848 10692607 5242880 83 Linux
  31.  
  32. Command (m for help): w
  33. The partition table has been altered!
  34.  
  35. Calling ioctl() to re-read partition table.
  36.  
  37. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
  38. The kernel still uses the old table. The new table will be used at
  39. the next reboot or after you run partprobe(8) or kpartx(8)
  40. Syncing disks.
  41. [root@ken ~]# partprobe
  42. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
  43. [root@ken ~]# ls /dev/sd*
  44. /dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2

第二步:格式化操作

使用SWAP分区专用的格式化命令mkswap,对新建的主分区进行格式化操作:

  1. [root@ken ~]# mkswap /dev/sdb2
  2. Setting up swapspace version 1, size = 5242876 KiB
  3. no label, UUID=92dbf6b7-4635-46a4-a813-0241098766d5

第三步:

使用swapon命令把准备好的SWAP分区设备正式挂载到系统中。我们可以使用free -m命令查看交换分区的大小变化

  1. [root@ken ~]# free -h
  2. total used free shared buff/cache available
  3. Mem: 974M 89M 755M 7.6M 129M 735M
  4. Swap: 2.0G 0B 2.0G
  5.  
  6. [root@ken ~]# swapon /dev/sdb2
  7. [root@ken ~]# free -h
  8. total used free shared buff/cache available
  9. Mem: 974M 93M 751M 7.6M 129M 731M
  10. Swap: 7.0G 0B 7.0G

第四步:写入到配置文件中

  1. [root@ken ~]# echo "/dev/sdb2 swap swap defaults 0 0" >> /etc/fstab

第五步:停止swap

使用swapoff可以停止swap

  1. [root@ken ~]# free -h
  2. total used free shared buff/cache available
  3. Mem: 974M 93M 751M 7.6M 129M 731M
  4. Swap: 7.0G 0B 7.0G
  5. [root@ken ~]# swapoff
  6.  
  7. Usage:
  8. swapoff [options] [<spec>]
  9.  
  10. Options:
  11. -a, --all disable all swaps from /proc/swaps
  12. -v, --verbose verbose mode
  13.  
  14. -h, --help display this help and exit
  15. -V, --version output version information and exit
  16.  
  17. The <spec> parameter:
  18. -L <label> LABEL of device to be used
  19. -U <uuid> UUID of device to be used
  20. LABEL=<label> LABEL of device to be used
  21. UUID=<uuid> UUID of device to be used
  22. <device> name of device to be used
  23. <file> name of file to be used
  24.  
  25. For more details see swapoff(8).
  26. [root@ken ~]# swapoff -a
  27. [root@ken ~]# free -h
  28. total used free shared buff/cache available
  29. Mem: 974M 88M 758M 7.6M 127M 737M
  30. Swap: 0B 0B 0B

swap的更多相关文章

  1. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  2. LVM 管理减少swap分区空间增加到根分区

    简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现 ...

  3. [LeetCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  4. 如何在Linux上使用文件作为内存交换区(Swap Area)

    交换区域(Swap Area)有什么作用? 交换分区是操作系统在内存不足(或内存较低)时的一种补充.通俗的说,如果说内存是汽油,内存条就相当于油箱,交换区域则相当于备用油箱. Ubuntu Linux ...

  5. XOR Swap

    swap(a, b): a ^= b b ^= a a ^= b 先明确一下,a ^ a = 0,同时对于一切数x ^ 0 = x 可以这样理解,第三行: b ^= a b ^= a ^ b b = ...

  6. 疑难问题解决备忘录(2)——ubuntu12.04分配swap

    分配swapdd if=/dev/zero of=Swap.disk bs=1M count=6k (count=1k创建1G的Swap,如果要创建6G则count=6k:这步比较慢) 创建swap文 ...

  7. Linux上的free命令详解、swap机制

    Linux上的free命令详解   解释一下Linux上free命令的输出. 下面是free的运行结果,一共有4行.为了方便说明,我加上了列号.这样可以把free的输出看成一个二维数组FO(Free ...

  8. centos 创建swap 交换分区

    阿里云的服务器是没有交换分区的,如 [www-data@iZbp1ivdq1ie5lmrhp13kjZ ~]$ free -m total used free shared buff/cache av ...

  9. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  10. CentOS7 SWAP 设置 (实测 笔记)

    首先查看当前的内存及swap情况(参数 -h,-m ) [root@centos ~]# free -h 查看swap信息,包括文件和分区的详细信息 [root@centos ~]# swapon - ...

随机推荐

  1. try? try! try do catch try 使用详解

    当一个使用一个方法发现后面 throws  说明可能会抛出异常 需要try 进行处理 1  try? 如果解析成功就有值 否则返回nil  (推荐) 2  try! 如果解析成功就有值  否则直接崩溃 ...

  2. Java Web 开发的JavaBean + Servlet + Sql Server

    日期:2018.12.9 博客期:026 星期日 我知道对于每个人都需要对开发web进行了解,而我们常用的技术,也应该有所了解 /*<------------------->*/知识点: ...

  3. django 中自定义过滤器

    多参数过滤器

  4. 进程与程序 并行 并发 串行 阻塞 join函数

    进程是正在运行的程序,程序是程序员编写的一对代码,也就是一堆字符,当这堆代码被系统加载到内存并执行,就有了进程. (需要注意的是:一个程序是可以产生多个程序,就像我们可以同时运行多个QQ程序一样,会形 ...

  5. C#关于线程的问题

    1.通过System.threading.Thread类可以创建新的线程,并在线程堆栈中运行静态和动态的实例,可以通过Thread类的构造方法传递一个无参数,并且不返回的委托, class Progr ...

  6. sqlmap+DWAV测试实战(一)

    root@kali-yaming:~# sqlmap -u "http://172.18.225.39/vulnerabilities/sqli/?id=1&Submit=Submi ...

  7. Software tips

    1.Microsoft office professional plus 2013秘钥 PD3W7-NVGG7-YKGQX-7CRDG-J2MG7(test success)MTGRC-8BM6H-W ...

  8. cf869C组合计数问题

    如果在两个区域里连点,两个区域内选的点数一定要相等 即a中选出i个点,必须与b中选出i个点相连 连接种类数为  然后我们再来看,如果ab中有两点相连,其中一点再与c相连会出事吗? 很显然不会对答案产生 ...

  9. bzoj 1951

    这道题告诉了我们一个很重要的道理:看到题,先想明白再动手! 题意:求对999911659取模的值 首先,由于n的数据范围不是很大(至少不是很大),所以可以O()枚举所有约数分别求组合数 但是有个问题: ...

  10. 在组件放使用v-model和slot插槽的简单实用

    封装的组件(SelectDefault.vue文件): <template> <div class="select-default"> <label& ...