jemalloc on linux from installation to use
redis useful to jemalloc.
First, what is jemalloc doing? Let's take a look at the author's own introduction:
jemalloc is a general purpose malloc(3) implementation that emphasizes fragmentation avoidance and scalable concurrency support
means that jemalloc does the work of malloc, and does a better job, mainly in avoiding memory fragmentation and concurrent expansion.
First, what is memory fragmentation?
malloc/free or new/delete is used in large quantities to cause memory fragmentation. What is the mechanism of this fragmentation? If the mechanism is formed by the size of the requested memory space (too small), then how much area can you apply to avoid memory fragmentation to the utmost extent? (The avoidance here is not an absolute avoidance, just a probability) Memory fragmentation is generally due to the fact that free space is less than the space to be applied, and these small memory blocks cannot be utilized. The method of generating memory fragmentation is very simple, for example: Suppose there is a continuous free memory space of 100 units, ranging from 0 to 99. If you apply for a piece of memory, such as 10 units, then the memory block applied for is 0~9. At this time, you continue to apply for a piece of memory, for example, 5 units are large, and the second block of memory blocks should be 10 to 14 intervals. If you release the first block of memory, then apply for a block of memory larger than 10 units, say 20 units. Because the memory block that was just released cannot satisfy the new request, only 20 units of memory blocks can be allocated starting from 15. Now the state of the entire memory space is 0~9 idle, 10~14 is occupied, 15~24 is occupied, and 25~99 is idle. 0~9 is a memory fragment. If 10~14 has been occupied, and the space for application in the future is more than 10 units, then 0~9 will never be used, resulting in wasted memory. If you apply for a memory that is smaller than the size of the previous village, the application will always succeed.
Memory fragment is terrible? Not obvious in a short time, for online servers such as nginx, the running time may be over the years, hundreds of millions of requests per day, if 100 pieces are produced every day, it is more than 30,000 pieces a year, quite scary!
jemalloc can effectively solve this problem! The performance map of jemalloc (already shown in the previous article):
Since jemalloc is so powerful, let's install it!
Download to the linux directory to extract:
tar -xjvf jemalloc-4.2.1.tar.bz2 The generated file list is as follows:
pre-compiled in this directory
make -j8 && make install
We go to the build directory to see:
generated header file in include, library In lib.
At this point, the installation is complete.
See below how to use: Directly point to jemalloc when compiling the link:
gcc -g -c -o 1.o 1.c gcc -g -o 1.out 1.o -L/usr/local/jemalloc/lib -ljemalloc
When you run the program, you may not find the library:
As long as the jemalloc library is found by the system:
echo /usr/local/jemalloc/lib >> /etc/ld.so.conf ldconfig