The Linux server allocates real and virtual memory to the processor, and manages the memory utilization through exchange. Understanding Linux memory types and how processors share memory can help optimize memory usage.
The total virtual memory is required to allocate memory from the Linux kernel start-up execution process. The virtual memory on Linux is infinite, and the processor can allocate memory at boot time. Linux kernel mapping can allocate virtual memory to real memory through exchange.
Unlike windows, memory swapping slows down programs. Linux memory swapping is beneficial, thanks to the way Linux analyzes the memory pages allocated by the process. The Linux kernel runs the most recently used algorithm to determine which memory pages need to enter ram and which do not. In some cases, making Linux kernel swapping faster can improve memory performance.
The process of a program usually needs more virtual memory than it actually needs. In Linux’s top-level programs, virtual memory requirements appear in virt and resident memory (RAA) volumes. When a process starts to do things with the memory pages allocated to it, the pages are moved to ram and displayed as RSS. Monitor the Linux kernel memory usage and let the administrator keep only these frequently used memory pages. Infrequently used access pages can be discarded through the kernel or exchange.
Processes on Linux servers use anonymous memory, file memory related to code and data. Anonymous memory must be available directly, so if memory resources are under pressure, pages labeled anonymous memory should be swapped.
Data memory is another thing. When a process reads data from disk, it remains in the cache, allowing the process to provide data quickly the next time it needs it. Under memory pressure, the kernel can discard data memory. There is no performance benefit to move unused file memory into the swap, and discarding is a better option.
When deciding to discard or exchange memory, the Linux kernel distinguishes between active and inactive memory. If inactive memory has not been used recently, there is no harm in moving it to swap or discarding it directly. /The proc / meminfo file shows the difference between anonymous and file memory active and inactive memory, as shown in Figure 1 below.
Figure 1: the difference between active and inactive memory from the Linux kernel
[[email protected] ~]# less /proc/meminfo
MemTotal： 7971636 kB
MemFree： 2653648 kB
Buffers： 250868 kB
Cached： 2974052 kB
SwapCached： 0 kB
Active： 2530380 kB
Inactive： 2323604 kB
Active（anon）： 1633532 kB
Inactive（anon）： 125712 kB
Active（file）： 896848 kB
Inactive（file）： 2197892 kB
Unevictable： 0 kB
Mlocked： 0 kB
SwapTotal： 10059772 kB
SwapFree： 10059772 kB
Dirty： 132 kB
Writeback： 0 kB
AnonPages： 1629052 kB
Mapped： 162396 kB
Shmem： 130192 kB
Slab： 296332 kB
SReclaimable： 233344 kB
SUnreclaim： 62988 kB
In this example, there is a lot of active anonymous memory and a small amount of inactive anonymous memory. If the system is under memory pressure, the kernel will move 125mb of inactive anonymous memory to the swap. 1.6gb of memory marked as active page memory should not be swapped because memory pages often need to be moved between ram and disk, which can degrade performance. If the amount of over swapping is greater than inactive (anonymous) memory, you should add more RAM to optimize Linux memory pages.
For an ideal Linux memory page, the total amount of swapping actually used should not exceed the amount of inactive (anonymous) memory pages. The amount of active (file) memory is equal to the amount of cache memory required by the kernel. Otherwise, you can’t improve server performance.
When the kernel needs more memory, the file memory marked inactive is discarded. Since the Linux kernel cannot handle it better, inactive file memory is kept in the cache. It doesn’t matter if the data is discarded. Then