Linux设置虚拟内存的教学与实战教程
什么是虚拟内存?
先直接摘抄一段 wikipedia 上的介绍。
虚拟内存是计算机系统内存管理的一种技术。它使得应用程序认为它拥有连续的可用的内存(一个连续完整的地址空间),而实际上,它通常是被分隔成多个物理内存碎片,还有部分暂时存储在外部磁盘存储器上,在需要时进行数据交换。
对于 c 语言里面的变量,我们可以使用 & 运算符来获得其地址, 既然是虚拟地址,就是指这个地址是虚拟的。
虚拟地址机制不是必须的,在简单的单片机中,编写的代码编译时都需要指定物理 ram 空间分布,不会有虚拟地址的概念,地址就是指在 ram 中的物理地址。
- 虚拟内存(之所以称为虚拟内存,是和系统中的逻辑内存和物理内存相对而言的,逻辑内存是站在进程角度看到的内存,因此是程序员关心的内容。而物理内存是站在处理器角度看到的内存,由操作系统负责管理。虚拟内存可以说是映射到这两种不同视角内存的一个技术手段。)技术就是一种由操作系统接管的按需动态内存分配的方法,它允许程序不知不觉中使用大于实际物理空间大小的存储空间(其实是将程序需要的存储空间以页的形式分散存储在物理内存和磁盘上),所以说虚拟内存彻底解放了程序员,从此程序员不用过分关心程序的大小和载入,可以*编写程序了,繁琐的事情都交给操作系统去做吧。
- swap(意思是“交换”、“实物交易”)分区是linux的交换分区。它的功能就是在内存不够的情况下,操作系统先把内存中暂时不用的数据,存到硬盘的交换空间,腾出内存来让别的程序运行,当请求的数据不在内存中时,系统产生却页中断,内存管理器便将对应的内存页重新从硬盘调入物理内存。(需要严重注意的是它和内存映射文件的区别,linux虚拟内存或者说交换分区是在磁盘中有一个指定的区域即swap。)交换分区实际存在于磁盘中,不过linux系统可以将它当作内存使用,当物理真实内存不足的时候交换分区就可以和真实内存进行数据交换。简单地说就是从磁盘里拿出一块空间当作内存的储备区。虽说磁盘被拿来当作内存使用,但是速度还是磁盘的速度。
介绍
在我们自己的购买的服务器环境中,一般是买的1g的内存,但是当服务器里面的东西装的比较多的时候就会导致内存不够用了,本文将模拟一个真实的内存不够用的情况下,如何通过修改虚拟内存来让系统正常运行,我们这里的环境是搭建一个elasticsearch搜索的环境,但是我们的服务器内存只有1g,下面将演示如何在将1g的虚拟内存修改为4g。
搭建elasticsearch环境
现在我们的服务器环境是空的,什么都没有,我们这里先将elasticsearch上传到服务器,然后将jdk和elasticsearch安装好。
安装jdk
安装教程后面更新(该文章主要介绍设置虚拟内存,安装这些东西主要是模拟一个内存不够的状态)
安装elasticsearch
安装链接后面更新(该文章主要介绍设置虚拟内存,安装这些东西主要是模拟一个内存不够的状态)
启动elasticsearch
启动elasticsearch,会发现启动的时候报错了,原因是我们的服务器现在的内存并不能满足elasticsearch需要的内存。
[esyonghu@localhost elasticsearch-6.4.0]$ ./bin/elasticsearch [1] 3228 [esyonghu@localhost elasticsearch-6.4.0]$ java hotspot(tm) 64-bit server vm warning: info: os::commit_memory(0x000000008a660000, 1973026816, 0) failed; error='cannot allocate memory' (errno=12) # # there is insufficient memory for the java runtime environment to continue. # native memory allocation (mmap) failed to map 1973026816 bytes for committing reserved memory. # an error report file with more information is saved as: # logs/hs_err_pid3228.log [esyonghu@localhost elasticsearch-6.4.0]$
查看我们的服务器的内存,使用命令 free,可以看到我们服务器的内存是1g, 这个时候就需要我们修改虚拟内存来解决该问题了。
[esyonghu@localhost elasticsearch-6.4.0]$ free -m total used free shared buffers cached mem: 980 582 397 2 23 245 -/+ buffers/cache: 313 667 swap: 0 0 0 [esyonghu@localhost elasticsearch-6.4.0]$
创建swap文件
进入/usr目录
[root@localhost usr]$ pwd /usr [root@localhost usr]$
创建swap文件夹,并进入该文件夹
[root@localhost usr]# mkdir swap [root@localhost usr]# cd swap/ [root@localhost swap]# pwd /usr/swap [root@localhost swap]#
创建swapfile文件,使用命令dd if=/dev/zero of=/usr/swap/swapfile bs=1m count=4096
[root@localhost swap]# dd if=/dev/zero of=/usr/swap/swapfile bs=1m count=4096 记录了4096+0 的读入 记录了4096+0 的写出 4294967296字节(4.3 gb)已复制,15.7479 秒,273 mb/秒 [root@localhost swap]#
查看swap文件
使用命令du -sh /usr/swap/swapfile,可以看到我们创建的这个swap文件为4g
[root@localhost swap]# du -sh /usr/swap/swapfile 4.1g /usr/swap/swapfile [root@localhost swap]#
将目标设置为swap分区文件
1、使用命令mkswap /usr/swap/swapfile将swapfile文件设置为swap分区文件
[root@localhost swap]# mkswap /usr/swap/swapfile mkswap: /usr/swap/swapfile: warning: don't erase bootbits sectors on whole disk. use -f to force. setting up swapspace version 1, size = 4194300 kib no label, uuid=5bd241ff-5375-449d-9975-5fdd429df784 [root@localhost swap]#
激活swap区,并立即启用交换区文件
使用命令swapon /usr/swap/swapfile
[root@localhost swap]# swapon /usr/swap/swapfile [root@localhost swap]#
使用命令free -m 来查看现在的内存,可以看到里面的swap分区变成了4095m,也就是4g内存。
[root@localhost swap]# free -m total used free shared buffers cached mem: 980 910 70 3 8 575 -/+ buffers/cache: 326 654 swap: 4095 0 4095 [root@localhost swap]#
设置开机自动启用虚拟内存,在etc/fstab文件中加入如下命令
1、使用vim编辑器打开/etc/fstab文件
2、在文件中加入如下内容
/usr/swap/swapfile2 swap swap defaults 0 0
使用reboot命令重启服务器
1、输入reboot 命令来重启
[root@localhost swap]# reboot broadcast message from liaocheng@localhost.localdomain (/dev/pts/1) at 3:56 ... the system is going down for reboot now! [root@localhost swap]# connection to 192.168.136.142 closed by remote host. connection to 192.168.136.142 closed. [进程已完成]
2、重启完成过后使用free -m 命令来查看现在的内存是否挂在上了。
[root@localhost swap]# free -m total used free shared buffers cached mem: 980 910 70 3 8 575 -/+ buffers/cache: 326 654 swap: 4095 0 4095
再次启动elasticsearch看看是否还会报内存不足的错误
1、还是切换到esyonghu去启动(这里为什么要使用es用户启动就先不介绍了,这是elasticsearch里面的知识,这里只是用elasticsearch来模拟内存不足的情况),可以看到已经不会有内存不足的问题了。
[esyonghu@localhost elasticsearch-6.4.0]$ ./bin/elasticsearch & [1] 2898 [esyonghu@localhost elasticsearch-6.4.0]$ [2019-03-06t04:00:24,841][info ][o.e.n.node ] [] initializing ... [2019-03-06t04:00:24,928][info ][o.e.e.nodeenvironment ] [dmy5nr5] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [7.6gb], net total_space [17.3gb], types [rootfs] [2019-03-06t04:00:24,928][info ][o.e.e.nodeenvironment ] [dmy5nr5] heap size [1.9gb], compressed ordinary object pointers [true] [2019-03-06t04:00:25,018][info ][o.e.n.node ] [dmy5nr5] node name derived from node id [dmy5nr5fthabb-q2t0txda]; set [node.name] to override [2019-03-06t04:00:25,018][info ][o.e.n.node ] [dmy5nr5] version[6.4.0], pid[2898], build[default/tar/595516e/2018-08-17t23:18:47.308994z], os[linux/2.6.32-696.el6.x86_64/amd64], jvm[oracle corporation/java hotspot(tm) 64-bit server vm/1.8.0_181/25.181-b13] [2019-03-06t04:00:25,018][info ][o.e.n.node ] [dmy5nr5] jvm arguments [-xms2g, -xmx2g, -xx:+useconcmarksweepgc, -xx:cmsinitiatingoccupancyfraction=75, -xx:+usecmsinitiatingoccupancyonly, -xx:+alwayspretouch, -xss1m, -djava.awt.headless=true, -dfile.encoding=utf-8, -djna.nosys=true, -xx:-omitstacktraceinfastthrow, -dio.netty.nounsafe=true, -dio.netty.nokeysetoptimization=true, -dio.netty.recycler.maxcapacityperthread=0, -dlog4j.shutdownhookenabled=false, -dlog4j2.disable.jmx=true, -djava.io.tmpdir=/tmp/elasticsearch.24q3s9ae, -xx:+heapdumponoutofmemoryerror, -xx:heapdumppath=data, -xx:errorfile=logs/hs_err_pid%p.log, -xx:+printgcdetails, -xx:+printgcdatestamps, -xx:+printtenuringdistribution, -xx:+printgcapplicationstoppedtime, -xloggc:logs/gc.log, -xx:+usegclogfilerotation, -xx:numberofgclogfiles=32, -xx:gclogfilesize=64m, -des.path.home=/home/esyonghu/elasticsearch-6.4.0, -des.path.conf=/home/esyonghu/elasticsearch-6.4.0/config, -des.distribution.flavor=default, -des.distribution.type=tar] [2019-03-06t04:00:28,022][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [aggs-matrix-stats] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [analysis-common] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [ingest-common] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [lang-expression] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [lang-mustache] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [lang-painless] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [mapper-extras] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [parent-join] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [percolator] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [rank-eval] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [reindex] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [repository-url] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [transport-netty4] [2019-03-06t04:00:28,023][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [tribe] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-core] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-deprecation] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-graph] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-logstash] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-ml] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-monitoring] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-rollup] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-security] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-sql] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-upgrade] [2019-03-06t04:00:28,024][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded module [x-pack-watcher] [2019-03-06t04:00:28,025][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded plugin [analysis-ik] [2019-03-06t04:00:28,025][info ][o.e.p.pluginsservice ] [dmy5nr5] loaded plugin [analysis-pinyin] [2019-03-06t04:00:31,315][info ][o.e.x.s.a.s.filerolesstore] [dmy5nr5] parsed [0] roles from file [/home/esyonghu/elasticsearch-6.4.0/config/roles.yml] [2019-03-06t04:00:32,017][info ][o.e.x.m.j.p.l.cpplogmessagehandler] [controller/2947] [main.cc@109] controller (64 bit): version 6.4.0 (build cf8246175efff5) copyright (c) 2018 elasticsearch bv [2019-03-06t04:00:32,495][debug][o.e.a.actionmodule ] using rest wrapper from plugin org.elasticsearch.xpack.security.security [2019-03-06t04:00:32,768][info ][o.e.d.discoverymodule ] [dmy5nr5] using discovery type [zen] [2019-03-06t04:00:33,628][info ][o.e.n.node ] [dmy5nr5] initialized [2019-03-06t04:00:33,628][info ][o.e.n.node ] [dmy5nr5] starting ... [2019-03-06t04:00:33,860][info ][o.e.t.transportservice ] [dmy5nr5] publish_address {192.168.136.142:9300}, bound_addresses {[::]:9300} [2019-03-06t04:00:33,884][info ][o.e.b.bootstrapchecks ] [dmy5nr5] bound or publishing to a non-loopback address, enforcing bootstrap checks [2019-03-06t04:00:36,995][info ][o.e.c.s.masterservice ] [dmy5nr5] zen-disco-elected-as-master ([0] nodes joined)[, ], reason: new_master {dmy5nr5}{dmy5nr5fthabb-q2t0txda}{ldgtz1xzsfopda9up4trea}{192.168.136.142}{192.168.136.142:9300}{ml.machine_memory=1028210688, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true} [2019-03-06t04:00:37,003][info ][o.e.c.s.clusterapplierservice] [dmy5nr5] new_master {dmy5nr5}{dmy5nr5fthabb-q2t0txda}{ldgtz1xzsfopda9up4trea}{192.168.136.142}{192.168.136.142:9300}{ml.machine_memory=1028210688, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true}, reason: apply cluster state (from master [master {dmy5nr5}{dmy5nr5fthabb-q2t0txda}{ldgtz1xzsfopda9up4trea}{192.168.136.142}{192.168.136.142:9300}{ml.machine_memory=1028210688, xpack.installed=true, ml.max_open_jobs=20, ml.enabled=true} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)[, ]]]) [2019-03-06t04:00:37,058][info ][o.e.x.s.t.n.securitynetty4httpservertransport] [dmy5nr5] publish_address {192.168.136.142:9200}, bound_addresses {[::]:9200} [2019-03-06t04:00:37,058][info ][o.e.n.node ] [dmy5nr5] started [2019-03-06t04:00:37,177][info ][o.w.a.d.monitor ] try load config from /home/esyonghu/elasticsearch-6.4.0/config/analysis-ik/ikanalyzer.cfg.xml [2019-03-06t04:00:37,179][info ][o.w.a.d.monitor ] try load config from /home/esyonghu/elasticsearch-6.4.0/plugins/ik/config/ikanalyzer.cfg.xml [2019-03-06t04:00:37,888][info ][o.e.m.j.jvmgcmonitorservice] [dmy5nr5] [gc][4] overhead, spent [486ms] collecting in the last [1.2s] [2019-03-06t04:00:38,435][warn ][o.e.x.s.a.s.m.nativerolemappingstore] [dmy5nr5] failed to clear cache for realms [[]] [2019-03-06t04:00:38,469][info ][o.e.l.licenseservice ] [dmy5nr5] license [c91cae39-79d7-4a0e-b40b-b1918a45f80c] mode [trial] - valid [2019-03-06t04:00:38,477][info ][o.e.g.gatewayservice ] [dmy5nr5] recovered [5] indices into cluster_state [2019-03-06t04:00:38,902][warn ][o.e.x.s.a.s.m.nativerolemappingstore] [dmy5nr5] failed to clear cache for realms [[]] [2019-03-06t04:00:39,106][info ][o.e.c.r.a.allocationservice] [dmy5nr5] cluster health status changed from [red] to [yellow] (reason: [shards started [[mynote2][2]] ...]).
2、现在使用free -m来查看内存使用情况, 可以看到swap已经被使用了1.7g
[esyonghu@localhost elasticsearch-6.4.0]$ free -m total used free shared buffers cached mem: 980 916 64 0 3 33 -/+ buffers/cache: 880 100 swap: 4095 1735 2360 [esyonghu@localhost elasticsearch-6.4.0]$
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。