6

高效内存分配器Jemalloc

 1 year ago
source link: https://garywu520.github.io/2023/01/29/%E9%AB%98%E6%95%88%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D%E5%99%A8Jemalloc/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

高效内存分配器Jemalloc

2023-01-29

字数统计: 304字

  |   阅读时长≈ 1分

Jemalloc介绍

Jemalloc是新一代内存分配器,它在FreeBSD被默认使用,侧重于减少内存碎片和提升高并发场景下内存的分配效率。Jemalloc应用十分广泛,在MySQL,Redis等场景中都有大量使用。

Github Release: Jemalloc

1. Install

yum install -y bzip2 autoconf automake libtool
wget https://github.com/jemalloc/jemalloc/releases/download/5.3.0/jemalloc-5.3.0.tar.bz2
bzip2 -d jemalloc-5.3.0.tar.bz2
tar -xf jemalloc-5.3.0.tar

cd jemalloc-5.3.0
./autogen.sh
./configure --prefix=/usr/local/jemalloc-5.3.0
make
make install

注:Linux动态库加载顺序

LD_PRELOAD > LD_LIBRARY_PATH > /etc/ld.so.cache > /lib > /usr/lib

所以可以看到,只要把动态库加载到LD_PRELOAD, 程序即可以优先启用Jemalloc

场景1: 仅在MySQL启用Jemalloc内存分配器

$ cat  /etc/supervisord.d/mysqld.conf
[program:mysqld]
environment=LD_PRELOAD="/usr/local/jemalloc-5.3.0/lib/libjemalloc.so" #添加此行变量
command = /usr/sbin/mysqld --defaults-file=/etc/my-prod.cnf --user=mysql
stdout_logfile = /var/log/mysqld.log
stderr_logfile = /var/log/mysqld.log
......
......

$ supervisorctl reread
$ supervisorctl update mysqld
#验证
$ lsof -p `pidof mysqld` | grep -i jemalloc

场景2: 为整个Linux系统启用Jemailoc动态库

$ cat /etc/ld.so.conf
/usr/local/jemalloc-5.3.0/lib/libjemalloc.so #放在文件首行
......


#立即永久生效
ldconfig

#验证
lsof -n | grep jemalloc

此时,CentOS系统中所有应用都已经使用了Jemailoc


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK