什么是虛擬內(nèi)存?
先直接摘抄一段 wikipedia 上的介紹。
虛擬內(nèi)存是計(jì)算機(jī)系統(tǒng)內(nèi)存管理的一種技術(shù)。它使得應(yīng)用程序認(rèn)為它擁有連續(xù)的可用的內(nèi)存(一個(gè)連續(xù)完整的地址空間),而實(shí)際上,它通常是被分隔成多個(gè)物理內(nèi)存碎片,還有部分暫時(shí)存儲(chǔ)在外部磁盤(pán)存儲(chǔ)器上,在需要時(shí)進(jìn)行數(shù)據(jù)交換。
對(duì)于 C 語(yǔ)言里面的變量,我們可以使用 & 運(yùn)算符來(lái)獲得其地址, 既然是虛擬地址,就是指這個(gè)地址是虛擬的。
虛擬地址機(jī)制不是必須的,在簡(jiǎn)單的單片機(jī)中,編寫(xiě)的代碼編譯時(shí)都需要指定物理 RAM 空間分布,不會(huì)有虛擬地址的概念,地址就是指在 RAM 中的物理地址。
介紹
在我們自己的購(gòu)買(mǎi)的服務(wù)器環(huán)境中,一般是買(mǎi)的1g的內(nèi)存,但是當(dāng)服務(wù)器里面的東西裝的比較多的時(shí)候就會(huì)導(dǎo)致內(nèi)存不夠用了,本文將模擬一個(gè)真實(shí)的內(nèi)存不夠用的情況下,如何通過(guò)修改虛擬內(nèi)存來(lái)讓系統(tǒng)正常運(yùn)行,我們這里的環(huán)境是搭建一個(gè)ElasticSearch搜索的環(huán)境,但是我們的服務(wù)器內(nèi)存只有1g,下面將演示如何在將1g的虛擬內(nèi)存修改為4G。
搭建ElasticSearch環(huán)境
現(xiàn)在我們的服務(wù)器環(huán)境是空的,什么都沒(méi)有,我們這里先將ElasticSearch上傳到服務(wù)器,然后將jdk和ElasticSearch安裝好。
安裝jdk
安裝教程后面更新(該文章主要介紹設(shè)置虛擬內(nèi)存,安裝這些東西主要是模擬一個(gè)內(nèi)存不夠的狀態(tài))
安裝ElasticSearch
安裝鏈接后面更新(該文章主要介紹設(shè)置虛擬內(nèi)存,安裝這些東西主要是模擬一個(gè)內(nèi)存不夠的狀態(tài))
啟動(dòng)ElasticSearch
啟動(dòng)ElasticSearch,會(huì)發(fā)現(xiàn)啟動(dòng)的時(shí)候報(bào)錯(cuò)了,原因是我們的服務(wù)器現(xiàn)在的內(nèi)存并不能滿(mǎn)足ElasticSearch需要的內(nèi)存。
[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]$
查看我們的服務(wù)器的內(nèi)存,使用命令 free,可以看到我們服務(wù)器的內(nèi)存是1g, 這個(gè)時(shí)候就需要我們修改虛擬內(nèi)存來(lái)解決該問(wèn)題了。
[esyonghu@localhost elasticsearch-6.4.0]$ free -m total used free shared buffers cachedMem: 980 582 397 2 23 245-/+ buffers/cache: 313 667Swap: 0 0 0[esyonghu@localhost elasticsearch-6.4.0]$
創(chuàng)建swap文件
進(jìn)入/usr目錄
[root@localhost usr]$ pwd/usr[root@localhost usr]$
創(chuàng)建swap文件夾,并進(jìn)入該文件夾
[root@localhost usr]# mkdir swap[root@localhost usr]# cd swap/[root@localhost swap]# pwd/usr/swap[root@localhost swap]#
創(chuàng)建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 的寫(xiě)出4294967296字節(jié)(4.3 GB)已復(fù)制,15.7479 秒,273 MB/秒[root@localhost swap]#
查看swap文件
使用命令du -sh /usr/swap/swapfile,可以看到我們創(chuàng)建的這個(gè)swap文件為4g
[root@localhost swap]# du -sh /usr/swap/swapfile4.1G /usr/swap/swapfile[root@localhost swap]#
將目標(biāo)設(shè)置為swap分區(qū)文件
1、使用命令mkswap /usr/swap/swapfile將swapfile文件設(shè)置為swap分區(qū)文件
[root@localhost swap]# mkswap /usr/swap/swapfilemkswap: /usr/swap/swapfile: warning: don't erase bootbits sectors on whole disk. Use -f to force.Setting up swapspace version 1, size = 4194300 KiBno label, UUID=5bd241ff-5375-449d-9975-5fdd429df784[root@localhost swap]#
激活swap區(qū),并立即啟用交換區(qū)文件
使用命令swapon /usr/swap/swapfile
[root@localhost swap]# swapon /usr/swap/swapfile[root@localhost swap]#
使用命令free -m 來(lái)查看現(xiàn)在的內(nèi)存,可以看到里面的Swap分區(qū)變成了4095M,也就是4G內(nèi)存。
[root@localhost swap]# free -m total used free shared buffers cachedMem: 980 910 70 3 8 575-/+ buffers/cache: 326 654Swap: 4095 0 4095[root@localhost swap]#
設(shè)置開(kāi)機(jī)自動(dòng)啟用虛擬內(nèi)存,在etc/fstab文件中加入如下命令
1、使用vim編輯器打開(kāi)/etc/fstab文件
2、在文件中加入如下內(nèi)容
/usr/swap/swapfile2 swap swap defaults 0 0
使用reboot命令重啟服務(wù)器
1、輸入reboot 命令來(lái)重啟
[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. [進(jìn)程已完成]
2、重啟完成過(guò)后使用free -m 命令來(lái)查看現(xiàn)在的內(nèi)存是否掛在上了。
[root@localhost swap]# free -m total used free shared buffers cachedMem: 980 910 70 3 8 575-/+ buffers/cache: 326 654Swap: 4095 0 4095
再次啟動(dòng)ElasticSearch看看是否還會(huì)報(bào)內(nèi)存不足的錯(cuò)誤
1、還是切換到esyonghu去啟動(dòng)(這里為什么要使用es用戶(hù)啟動(dòng)就先不介紹了,這是elasticsearch里面的知識(shí),這里只是用elasticsearch來(lái)模擬內(nèi)存不足的情況),可以看到已經(jīng)不會(huì)有內(nèi)存不足的問(wèn)題了。
[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、現(xiàn)在使用free -m來(lái)查看內(nèi)存使用情況, 可以看到swap已經(jīng)被使用了1.7G
[esyonghu@localhost elasticsearch-6.4.0]$ free -m total used free shared buffers cachedMem: 980 916 64 0 3 33-/+ buffers/cache: 880 100Swap: 4095 1735 2360[esyonghu@localhost elasticsearch-6.4.0]$
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選