How to make the system allocate more memory to mysql under Linux?

Linux processes apply for memory from the system through the memory allocation function malloc in the C standard library, but there is actually another layer between them and the kernel, that is, the memory allocator. Common memory allocators are ptmalloc(Glibc), tcmalloc(Google) and jemalloc(FreeBSD). MySQL uses ptmalloc of glibc as the memory allocator by default.

Memory allocator adopts the management mode of memory pool, which is located between user program layer and kernel layer. It responds to the user's allocation request, applies for memory from the operating system, and then returns it to the user program.

In order to maintain efficient allocation, the allocator usually requests a piece of memory from the operating system in advance. When the user program applies for and releases the memory, the allocator will manage the memory and decide whether to return it to the operating system through some algorithm strategies. The biggest advantage of this is that it can avoid user programs frequently calling the system to allocate memory, so that user programs can use memory more efficiently and quickly.

Personally, I don't know much about the memory allocation principle of ptmalloc, so I won't teach the axe to swim here. Interested students can look at Hua Ting's glibc memory management ptmalloc source code analysis.

Regarding how to choose these three memory allocators, most online materials recommend abandoning glibc's native ptmalloc and using jemalloc or tcmalloc as the default allocator. Because the main problems of ptmalloc are actually the performance problems caused by memory waste, memory fragmentation and locks, jemalloc and tcmalloc are more suitable for memory fragmentation and multithreading.

Jemalloc is currently used in Firefox, FaceBook and so on. , and it is the memory allocator recommended by MariaDB, Redis and Tengine by default, while tcmalloc is applied to WebKit, Chrome and so on.