In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
How to carry out the theoretical analysis of SWAP knowledge, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Swap space in Linux is used when the amount of physical memory (RAM) is full.
If the system needs more memory resources and the RAM is full, the inactive pages in memory will be moved to swap space.
While swap space can help computers with a small amount of RAM, it should not be seen as a substitute for more RAM.
The swap space is located on the hard disk, and the access time of the hard disk is slower than the physical memory. Swap space can be a private swap partition (recommended), swap file, or a combination of swap partition and swap file.
In order to improve the efficiency and speed of reading and writing, the Linux kernel will cache files in memory, which is called Cache Memory (cache memory). Even after your program is finished, Cache Memory will not be released automatically. This will cause you to find less physical memory available after frequently reading and writing files in your Linux system. When the user submits the program and then generates the process and runs it on the machine, the machine will judge whether the current physical memory is free to allow the process to be called into memory to run, and if so, directly into memory to run. If not, a process is selected to suspend according to its priority, swapped to swap for waiting, and some of the physical memory occupied by the program is freed, and the new process is called into memory to run. Wait until the suspended program is about to run, and then restore the saved data from the Swap partition to memory.
But not all data swapped from physical memory will be put into Swap (if so, Swap will be overwhelmed), and a considerable amount of data will be exchanged directly to the file system. For example, some programs will open some files to read and write files (in fact, each program has to open at least one file, that is to run the program itself). When you need to swap out the memory space of these programs, there is no need to put the data of the file part into the Swap space, but you can put it directly into the file. If it is a read file operation, then the in-memory data is released directly and does not need to be swapped out, because the next time it is needed, it can be directly recovered from the file system; if it is a write file, only the changed data needs to be saved to the file for recovery. An error out of memory will be reported when both physical memory and swap are used up.
Daily examples of Swap being used
When using the Windows system, you can run multiple programs at the same time, and when you switch to a program that you haven't paid attention to for a long time, you will hear the hard drive "clatter". This is because the program's memory is "stolen" by frequently running programs and placed in the Swap area. Therefore, once the program is placed on the front end, it will retrieve its data from the Swap area, put it in memory, and then run it.
The function of Swap Space
When the physical memory of the system is insufficient, a portion of the physical memory needs to be released for use by the currently running program. The free space may come from programs that have been inactive for a long time, which are temporarily saved in Swap space, and then recover the saved data from Swap to memory when those programs are about to run. From this, we can also see that swap plays a very important role in temporarily storing the process of being swapped out. According to this kind of swapping in and out, the recycling of memory is realized, so that users can not feel the limitation of memory.
Impact of Swap Partition on performanc
Square: Swap cannot be used instead of physical memory! Because swap is still disguised as memory by disk after all, the swap operation is the operation of disk IO rather than the load and store operation of memory.
On the contrary: under normal circumstances, when there is enough physical memory, swap memory area is rarely used, and when there is not enough physical memory, the data stored in swap space is also data that the system will not use frequently, and the number of calls is very small, and performance problems can almost be ignored. If physical memory is extremely insufficient, what is the relationship between performance problems and Swap partitions? at this time, it is useless for you to add most Swap partitions, you can only add physical memory.
After using what proportion of memory, start using swap.
It can be adjusted in the / proc/sys/vm/swappiness file, which defaults to 60. This parameter can be set from 0-100. 0 is to maximize the use of memory, try not to use swap;100 is to actively use swap. 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
Temporary modification of swappiness parameters (cannot be directly vi / proc/sys/vm/swappiness), invalidated after restart
Method 1:echo 60 > / proc/sys/vm/swappiness
Method 2:sysctl vm.swappiness=60
The method of permanently modifying swappiness parameters, which takes effect after restart
Modify the vm.swappiness in / etc/sysctl.conf, you can directly vi / etc/sysctl.conf
Turn swap swap on and off
Swapoff / dev/dm-1
Swapon / dev/dm-1
The swap exchange partition is not the / dev/shm partition corresponding to tmpfs, but the partition of Filename corresponding to swapon-s
[root@localhost] # swapon-s
Filename Type Size Used Priority
/ dev/dm-1 partition 4194296 42352-1
[root@localhost ~] # ll / dev/mapper/VolGroup-lv_swap
Lrwxrwxrwx 1 root root 7 June 27 10:34 / dev/mapper/VolGroup-lv_swap->.. / dm-1
[root@localhost ~] # fdisk-l | grep swap
Disk / dev/mapper/VolGroup-lv_swap: 4294 MB, 4294967296 bytes
[root@localhost ~] # ll / dev | grep dm-1
Brw-rw---- 1 root disk 253, June 27 10:34 dm-1
[root@localhost] # df-h
Filesystem Size Used Avail Use% Mounted on
/ dev/mapper/VolGroup-lv_root 35G 3.6G 30G 11% /
Tmpfs 3.9G 72K 3.9G 1% / dev/shm
/ dev/sda1 485m 39m 421m 9% / boot
/ dev/sdb1 197G 100G 87G 54% / db
How to determine the size of Swap
The CENTOS official website adopts the following formula:
M = Amount of RAM in GB
S = Amount of swap in GB
Then If M
< 2, S = M *2 Else S = M + 2。 ORACLE的官方文档推荐 RAM ==>Swap Space
Up to 512 MB = > 2 times the size of RAM
Between 1024 MB and 2048 MB== > 1.5 times the size of RAM
Between 2049 MB and 8192 MB== > Equal to the size of RAM
More than 8192 MB = > .75 times the size of RAM
Commands for viewing Swap memory
Free and swapon-s and cat / proc/swaps can all see the approximate Swap information.
Swapon-s and cat / proc/swaps can also see the disk information corresponding to Swap.
Vmstat can see the size of Swap in and Swap out during the interval.
[root@localhost ~] # free-k | grep Swap
Swap: 4194296 42352 4151944
[root@localhost] # swapon-s
Filename Type Size Used Priority
/ dev/dm-1 partition 4194296 42352-1
[root@localhost ~] # cat / proc/swaps
Filename Type Size Used Priority
/ dev/dm-1 partition 4194296 42352-1
[root@localhost] # free-m
Total used free shared buffers cached
Mem: 7872 6580 1292 0 104 1037
-/ + buffers/cache: 5339 2433
Swap: 4095 3241 854
Second line
Total = used + free
The third line
(- buffers/cache) used memory: the used-buffers-cached in the first part of the Mem line (reflecting the memory actually used by the program)
(+ buffers/cache) free memory: free + buffers + cached in the first part of the Mem line (reflecting the total amount of memory that can be misappropriated)
Fourth line
Refers to the SWAP switching partition
1) if there is no swap:
Swap used 3241m, and the free physical memory is 2433m, that is, without swap, the memory would have exploded.
2) swap does not affect performance:
The system still has enough physical memory (2433m), in other words, the data stored in swap is still infrequently used (that is, swap is playing a normal role).
[root@localhost ~] # vmstat 3
Procs-memory--swap---io-----system---cpu-
R b swpd free buff cache si so bi bo in cs us sy id wa st
00 42352 1322484 209984 6182296 00 17 21 4 3 00 100 00
00 42352 1322484 209984 6182296 00 00 42 34 00 100 00
00 42352 1322484 209984 6182296 00 00 38 36 00 100 00
00 42352 1322484 209984 6182296 00 00 61 43 00 100 00
00 42352 1322484 209984 6182296 00 00 34 33 00 100 00
Vmstat 3 means to collect every 3 seconds
Si represents the total amount of memory (Swap in) swapped back (within three seconds), in kbytes
So represents the total amount of memory (Swap out) swapped out currently (within three seconds), in kbytes.
After reading the above, have you mastered the method of theoretical analysis of SWAP knowledge? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.