The last article said our performance optimization of THE CPU dimension of the optimization direction, and CPU indicators of the basic knowledge of performance optimization CPU, this article we begin, from the perspective of memory to explain the direction of optimization.

The memory principle

Memory generalization

What is the memory of everyday life

  • For example, my laptop has 8GB of ram
  • This memory is actually physical memory
  • Physical memory is also called main memory, and the main memory used by most computers is dynamic random access memory (DRAM)

Virtual address space

The Linux kernel provides a separate, contiguous virtual address space for each process. In this way, the process can easily access memory, or more specifically virtual memory.

Inside the virtual address space

  • The interior of the virtual address space is divided into kernel space and user space

  • Processors with different word lengths (the maximum length of data that can be processed by a single CPU instruction) have different ranges of address Spaces, such as 1 gigabyte of kernel space at the top of a 32-bit system and 3 gigabytes of user space

64-bit systems have 128 terabytes of kernel and user space, occupying the highest and lowest parts of the total memory space, respectively, leaving the middle part undefined

The user and kernel states of the process

In user mode, a process can only access user space memory. Kernel space memory can only be accessed after entering kernel state. Although each process’s address space contains kernel space, these kernel Spaces are actually associated with the same physical memory. In this way, the process can easily access kernel space memory after switching to kernel mode.

Why memory mapping

Since each process has such a large address space, the virtual memory of all processes combined is naturally much larger than the physical memory in the real world. Therefore, not all virtual memory is allocated physical memory, only the virtual memory that is actually used is allocated physical memory, and the allocated physical memory is managed through memory mapping.

What is memory mapping

Memory mapping refers to mapping virtual memory addresses to physical memory addresses. To accomplish memory mapping, the kernel maintains a page table for each process that maps virtual addresses to physical addresses, as shown below:

Operation Principle of SWAP

Swap uses a disk space or a local file as memory. It involves swapping out and swapping in.

  • The so-called swap out is to store temporarily unused memory data to the disk and release the memory occupied by these data.
  • Swapping, on the other hand, is reading from disk to memory when the process accesses the memory again.

A typical scenario is that some applications don’t want to be killed by OOM even if they run out of memory. Instead, they want to wait for manual intervention or for the system to automatically free up the memory of other processes and reallocate it. In addition, the sleep and fast boot functions of our common laptops are also based on Swap. When hibernating, save the system’s memory to disk so that when you boot up again, you can simply load memory from disk. This saves a lot of application initialization and speeds up startup. Then again, if Swap is about reclaiming memory, when exactly does Linux need to reclaim memory? Memory resources have been said in front of the tight, and how to measure memory is tight?

One of the most obvious scenarios is when there is a request for a new chunk of memory allocation, but there is not enough memory left. At this point, the system needs to reclaim some memory to meet as many new memory requests as possible. This process is often referred to as direct memory reclamation.

In addition to direct memory reclamation, there is also a special kernel thread for periodic memory reclamation, known as KSWAPd0. To measure memory usage, kSWAPd0 defines three memory thresholds (pages_min), pages_low, and pages_high. The remaining memory is pages_free

Kswapd0 periodically scans the memory usage and reclaims the remaining memory based on the three thresholds.

  • If the remaining memory is less than the minimum page threshold, the process has used up all available memory and only the kernel can allocate memory.
  • If the remaining memory falls between the minimum page threshold and the low page threshold, the memory pressure is high and the remaining memory is insufficient. Kswapd0 then performs memory reclamation until the remaining memory is greater than the high threshold.
  • If the remaining memory falls between the low page threshold and the high page threshold, it indicates that the memory is under certain pressure but can still meet new memory requests.
  • If the remaining memory is greater than the page height threshold, the remaining memory is large and there is no memory pressure.

As you can see, once the remaining memory is below the page low threshold, a reclamation of memory is triggered. This page low threshold can actually be set indirectly with the kernel option /proc/sys/vm/min_free_kbytes. Min_free_kbytes sets the minimum page threshold. The other two thresholds are generated based on the minimum page threshold. The calculation method is as follows:

pages_low = pages_min*5/4
pages_high = pages_min*3/2
Copy the code

Memory performance statistics

Memory usage & tuning
  • free

All values default to bytes (KB)

  • First line Mem: physical memory; Line 2 Swap: Swap partition

free = total – used – shared – buffcache

Example: Output statistics every 2s, output a total of two times, and output all values


[root@centos7-2 ~]# free -h -c 2 -s 2

Copy the code
  • The top command

Performance analysis

Memory Performance Specifications

  1. System memory usage

For example, used memory, remaining memory, shared memory, available memory, cache and buffer usage.

  • Used memory and remaining memory are easily understood as used and unused memory.

  • Shared memory is implemented via TMPFS (file system of memory), so its size is the same as the memory used by TMPFS. TMPFS actually

It’s also a special kind of cache.

  • Free memory is the maximum memory that a new process can use, and it includes both the remaining memory and the recyclable cache.

  • The cache consists of two parts, one is the page cache of disk-read files, which is used to cache data read from disk so that it can be accessed more quickly later

Speed. The other part is the recoverable memory in the Slab allocator.

  • Buffers are temporary storage of raw disk blocks used to cache data that will be written to disk. In this way, the kernel can cluster the scattered writes

To uniformly optimize disk writes.

2. Process memory usage, such as virtual memory, resident memory, shared memory, and Swap memory

  • Virtual memory includes process code segment, data segment, shared memory, heap memory that has been applied and memory that has been swapped out, etc. And notice here,

Allocated memory, even if no physical memory has been allocated, counts as virtual memory.

  • Resident memory is the physical memory actually used by the process, however, it does not include Swap and shared memory.

  • Shared memory includes not only the real shared memory used by other processes, but also the loaded dynamic link library and code snippet of the program

Swap memory refers to the memory that is swapped out to a disk through Swap

3. Abnormal page missing

After the system calls the memory allocation request, it does not allocate physical memory for it immediately, but through the missing page when the request is first accessed

Often to assign. Page – missing exceptions are divided into the following two scenarios.

  • When it can be allocated directly from physical memory, it is called a secondary page – missing exception.

  • When disk I/O intervention (such as Swap) is required, it is called a major page failure.

4.Swap usage

For example, the used space, remaining space, Swap speed, and Swap speed of Swap

  • Used space and free space are well understood as literally used and unused memory space.

  • Swap in and swap out speed, is the size of swap in and swap out memory per second.

Memory tuning strategies

There are several common optimization ideas.

1) It is better to disable Swap. If you must enable Swap, reduce the swappiness value to reduce the tendency to use Swap during memory reclamation.

2) Reduce the dynamic allocation of memory. For example, you can use memory pools, hugepages, and so on.

3) Use caches and buffers whenever possible to access data. For example, you can use a stack to explicitly declare memory space to store data that needs to be cached. Or use

External caching components, such as Redis, optimize access to data.

4) Use cgroups to limit the memory usage of the process. This ensures that system memory will not be exhausted by abnormal processes.

5) Use /proc/pid/oom_adj to adjust the oOM_score of the core application. This ensures that core apps won’t be killed by the OOM even if memory is tight.