System performance analysis-memory

Memory allocation and recovery

malloc() is a memory allocation function provided by the C standard library. Corresponding to the system call, there are two implementation methods, brk() and mmap() .

For small pieces of memory ( less than 128K) , the C standard library uses brk() to allocate, that is, allocates memory by moving the top of the heap. After the memory is released, it will not be returned to the system immediately, but will be cached. It can be reused.

Large blocks of memory ( greater than 128K) are directly allocated using mmap() , that is, a block of memory is directly allocated in the file mapping section.

These two methods have their own advantages and disadvantages.

The brk() method of caching can reduce the occurrence of page faults and improve memory access efficiency. However, since these memories are not returned to the system, frequent memory allocation and release will cause memory fragmentation when the memory is busy.

The memory allocated by mmap() method will be returned to the system when the memory is released, so mmap() method will cause page fault exceptions. When the memory is busy, frequent memory allocation will cause a large number of page fault exceptions and make the management of the kernel The burden increases. This is why malloc only uses mmap() for large memory blocks . After understanding these two calling methods, you also need to understand clearly that when these two calls occur, memory is not actually allocated. These memories are allocated only when they are accessed for the first time, that is, they enter the kernel through a page fault exception, and then the kernel allocates the memory.

In general, Linux uses a partner system to manage memory allocation. As mentioned earlier, these memories are managed in units of pages in the MMU , and the partner system is the same, managing memory in units of pages, and will reduce memory fragmentation by merging adjacent pages ( such as fragmentation generated by brk ) .

If you encounter an object smaller than a page, such as less than 1K , how to allocate memory?

In user space, malloc() allocates memory through brk() and does not return it to the system immediately when it is released, but caches it for reuse. In the kernel space, Linux manages small memory through the slab allocator.

For memory, if it is only allocated but not released, it will cause memory leaks and even exhaust system memory. So after the application runs out of memory, it needs to call free() or unmap() to release these memory resources. Of course, the system will not let an application run out of system memory. When memory resources are found to be tight, the system will reclaim memory through a series of mechanisms, as follows :

1. Reclaim the cache: For example, use the LRU (Last Recently Used) algorithm to reclaim the recently used pages.

2. Reclaim the rarely accessed memory, and write the rarely accessed memory to the disk through the swap partition.

3. Kill the process. When the memory is tight, the system will directly kill the process that takes up a lot of memory through OOM (Out Of Memory) .

The second method uses swap . Swap actually uses a piece of disk space as memory. It can store data temporarily not used by the process on the disk ( this process is called swapping out ) . When the process accesses the data, The disk is read into memory ( this process is called swap-in ) . So you can see swap to expanded memory, memory is not enough only to pay attention to, will occur swap exchange , and the disk read and write speeds slower than memory, memory can cause performance problems.

The third mentioned OOM , which is actually a protection mechanism of the kernel. It monitors the memory usage of processes and uses oom_score to score the memory usage of each process :

A process consumes more memory, oom_score greater

A process running occupation of CPU more, oom_score less

In this way , the larger the oom_score of the process , the more memory it consumes, and the easier it is to be killed by OOM , thus better protecting the system. Of course, when the actual work requires, the administrator can also manually set the oom_adj of the process through the /proc file system to adjust the oom_score of the process . oom_adj in the range [-17,15], the larger the value, the more easily OOM killed, the smaller the value, the more difficult the OOM killed, where -17 indicating the prohibition OOM .

For example, use the following command to adjust the oom_adj of the sshd process to -16 , so that the sshd process will not be easily killed by OOM .

echo -16 >/proc/$(pidof sshd)/oom_adj

proc file system

/proc is a special file system provided by the Linux kernel, an interface for system users to interact with the kernel. For example, users can query the running status and configuration options of the kernel from /proc , query the running status of the process, statistics, etc., or modify the configuration of the kernel through /proc . The /proc file system is also the final data source for many performance tools. For example, free , is to obtain memory usage by reading /proc/meminfo . By man free

Buffers

memory used by kernel buffers (Buffers in /proc/meminfo)

cache  

memory used by the page cache and slabs (Cached and Slab in /proc/meminfo)

Memory performance index

The first is system memory usage, such as used memory, remaining memory, shared memory, available memory, cache and buffer usage, etc.

* Used memory and remaining memory are used and unused memory

* Shared memory is by tmpfs system implementation, it is the size of tmpfs use of memory size, tmpfs is actually a special kind of cache.

* Available memory is the maximum memory that a new process can use, it includes remaining memory and reclaimable cache

*The cache consists of two parts, one is the page cache for reading files from the disk, which is used to cache the data read from the disk, which can speed up the re-access; the other part is the reclaimable memory in the slab allocator

* Buffer is a temporary storage of original disk blocks, used to cache data to be written to disk. It is convenient for the kernel to gather scattered writes and optimize disk writes uniformly

The second is process memory usage, such as process virtual memory, resident memory, shared memory and swap memory, etc.

* Virtual memory includes process code segment, data segment, shared memory, heap memory that has been applied for, memory that has been swapped out, etc. ( Memory that has been applied for is counted as virtual memory even if no physical memory is allocated ) .

* Resident memory is the physical memory actually used by the process, but it does not include swap and shared memory

*swap memory, memory swapped out to disk through swap

Among these indicators, resident memory is generally converted into a percentage of the total system memory, which is the memory usage of the process

According to the principle of memory allocation, after the system calls the memory allocation request, it does not allocate memory for it immediately, but allocates it through a page fault exception when the first access is requested. Page faults are divided into the following two situations:

*When it can be allocated directly from physical memory, it is called a secondary page fault exception

*When disk I/O intervention (swap) is required, it is called the main page fault exception

Obviously, the abnormal increase of main page faults means that disk I/O is required , and memory access will be much slower.

第三类指标是swap使用情况,比如swap的已用空间、剩余空间、换入速度、换出速度等

*已用空间和剩余空间就是已经使用和没有使用的内存空间

*换入速度和换出速度则是每秒换入和换出内存的大小

如何快速分析出内存的性能瓶颈

内存性能指标很多,但都是为了描述内存的基本原理,指标之间都有一定的关联。为了迅速定位内存问题,通常先运行几个覆盖面比较大的性能工具,比如freetopvmstatpidstat等,主要分析思路如下:

1、现用freetop查看系统内存整体使用情况

2、再用vmstatpidstat,查看一段时间的趋势,从而判断出内存问题的类型

3、最后进行详细分析,比如内存分配分析、缓存/缓冲区分析、具体进程的内存使用分析等。

Guess you like

Origin blog.51cto.com/13162375/2545296