Linux setting up virtual memory teaching and practical tutorial

  • 2021-01-03 21:14:44
  • OfStack

What is virtual memory?

First, directly extract the introduction from paragraph 1 of wikipedia.

[

Virtual memory is a technique of memory management in computer system. It makes the application think that it has contiguous available memory (a contiguous complete address space), when in fact it is usually partitioned into multiple physical memory fragments, with some temporarily stored on external disk storage for data exchange as needed.

]

For variables in the C language, we can use & Operator to get its address, which, since it is a virtual address, means the address is virtual.

Virtual address mechanism is not necessary. In a simple single chip microcomputer, the compiled code all needs to specify physical RAM spatial distribution. There is no concept of virtual address, and the address refers to the physical address in RAM.

Virtual memory (called virtual memory) is in contrast to the logical memory in the system and the physical memory. Logical memory is the memory seen from the process perspective and is therefore of concern to programmers. The physical memory is the memory seen from the processor's point of view and managed by the operating system. Virtual memory can be said to be mapped to these two different perspectives of memory 1 technique. Technology is one kind by the operating system to take over the on-demand dynamic memory allocation method, it allows application unconsciously use is greater than the actual physical space of the size of the storage space (in fact is the program need storage space in the form of pages stored separately in physical memory and disk), so the virtual memory completely liberated programmers, programmers don't use too much concerned about the program from now on and the size of the load, free program, trival things to the operating system to do. The SWAP(meaning "exchange", "barter") partition is the swap partition of Linux. The function of it is in memory of the case, the operating system the data in memory is not used temporarily, first deposit to hard disk swap space, free up memory to let the other program is running, when the request data is not in memory, the system produces page was interrupted, the memory manager to the corresponding memory pages to transfer into physical memory from the hard disk. (Important to note is that it differs from memory-mapped files in that the linux virtual memory or swap partition has a designated area on the disk called swap.) The swap partition actually exists on disk, but the Linux system can use it as memory and swap data with real memory when physical real memory is out of stock. To put it simply, take 1 block of disk space as the storage area of memory. Although the disk is used as memory, the speed is still the speed of the disk.

introduce

Purchased in our own server environment, 1 kind is to buy 1 g memory, but when the server of the contents of more time can cause memory use, this paper simulates a real memory use cases, how to modify the virtual memory to make the system run normally, we search the environment here is to build a ElasticSearch environment, but we have only 1 g server memory, below will demonstrate how to amend the 1 g virtual memory to 4 G.

Build ElasticSearch environment

Now that our server environment is empty and there is nothing, we will upload ElasticSearch to the server and then install jdk and ElasticSearch.

Install jdk

The installation tutorial will be updated later (this article is mainly about setting up virtual memory, which is mainly to simulate the state of 1 out of memory).

Install ElasticSearch

The installation link will be updated later (this article is mainly about setting up virtual memory, which is mainly used to simulate the state of 1 memory shortage).

Start the ElasticSearch

When you start ElasticSearch, you will find that there is an error when you start ElasticSearch, because our server does not have enough memory to meet the needs of 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]$ 

Looking at the memory of our server, using the command free, you can see that the memory of our server is 1g. At this time, we need to modify the virtual memory to solve the problem.


[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]$ 

Create the swap file

Go to the /usr directory


[root@localhost usr]$ pwd
/usr
[root@localhost usr]$ 

Create the swap folder and enter it


[root@localhost usr]# mkdir swap
[root@localhost usr]# cd swap/
[root@localhost swap]# pwd
/usr/swap
[root@localhost swap]# 

To create the swapfile file, use the command dd if=/dev/zero of=/usr/swap/ bs=1M count=4096


[root@localhost swap]# dd if=/dev/zero of=/usr/swap/swapfile bs=1M count=4096
 Record the 4096+0  The read 
 Record the 4096+0  The write 
4294967296 byte (4.3 GB) Has been copied, 15.7479  Second, 273 MB/ seconds 
[root@localhost swap]#

View the swap file

Using the command du sh/usr/swap/swapfile, you can see we created this is 4 g swap file


[root@localhost swap]# du -sh /usr/swap/swapfile
4.1G	/usr/swap/swapfile
[root@localhost swap]# 

Set the target to the swap partition file

1. Use the command mkswap /usr/swap/swapfile to set the swapfile file as swap partition file


[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]#

Activate the swap area and enable the swap file immediately

Use the command swapon /usr/swap/swapfile


[root@localhost swap]# swapon /usr/swap/swapfile
[root@localhost swap]#

Using the command ES133en-ES134en to view the current memory, you can see that the Swap partition inside has changed to 4095M, which is 4G memory.


[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]#

Set boot to enable virtual memory automatically and add the following command to the etc/fstab file

1. Use the vim editor to open /etc/fstab file

2. Add the following to the file


/usr/swap/swapfile2 swap swap defaults 0 0

Restart the server using the reboot command

1. Type reboot to restart


[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]$ 
0

2. After the restart, use the ES160en-ES161en command to check whether the current memory is hung on.


[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]$ 
1

Start ElasticSearch again to see if you still report an out-of-memory error

1, or switch to esyonghu to boot (here why to use es user boot will not be introduced, this is the knowledge in elasticsearch, here is only to use elasticsearch to simulate the situation of insufficient memory), you can see that there is no problem of insufficient memory.


[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]$ 
2

2. Now use ES175en-ES176en to check the memory usage, you can see that swap has been used 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]$

conclusion


Related articles: