Jemalloc优化MySQL、Nginx/Tengine内存管理

[隐藏]

《Jemalloc优化MySQL、Nginx/Tengine内存管理》

Jemalloc源于Jason Evans 2006年在BSDcan conference发表的论文:《A Scalable Concurrent malloc Implementation for FreeBSD》

安装Jemalloc

源码下载地址:
http://www.canonware.com/download/jemalloc/
https://github.com/jemalloc/jemalloc/

cd /usr/locla/src
wget http://www.canonware.com/download/jemalloc/jemalloc-4.2.1.tar.bz2
tar xjf jemalloc-4.2.1.tar.bz2
cd jemalloc-4.2.1
./configure
make && make install

echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig

使用Jemalloc优化Nginx

重新编译nginx

cd /usr/local/src
cd nginx-1.10.1
make clean
./configure --prefix=/usr/local/nginx-1.10.1 --user=nobody --group=nobody \
--with-http_stub_status_module --with-http_ssl_module --with-http_flv_module \
--with-http_gzip_static_module --with-ld-opt="-ljemalloc"  --with-http_mp4_module 

make && make install

验证jemalloc优化 Nginx是否生效,如下

lsof -n | grep jemalloc

使用Jemalloc优化Mysql

mysql 是通过yum安装的,通过sed命令在mysqld_safe文件中添加,需要注意mysqld_safe的位置

sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libjemalloc.so@' /usr/bin/mysqld_safe

service mysqld restart

#看到文件中添加新的行就证明添加成功了
vim /usr/bin/mysqld_safe
# executing mysqld_safe
export LD_PRELOAD=/usr/local/lib/libjemalloc.so

验证jemalloc优化mysql是否生效,如下

[root@loalhost lib]# lsof -n | grep jemalloc 
nginx     13186      root  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
nginx     13187    nobody  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
nginx     13188    nobody  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
nginx     13190    nobody  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
nginx     13191    nobody  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
nginx     13192    nobody  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
nginx     13193    nobody  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
nginx     13194    nobody  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
nginx     13195    nobody  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2
mysqld    32349     mysql  mem       REG               8,18     7341639     156595 /usr/local/lib/libjemalloc.so.2

总结

nginx需要重新安装, MySQL需要注意mysqld_safe文件安装的位置。以上安装都是在CentOS6.x环境下执行 。

点赞
  1. heguangzhi说道:

    :lol: :lol: :rolleyes: :rolleyes:

发表评论

电子邮件地址不会被公开。 必填项已用*标注