Tcmalloc optimized Nginx, MySQL, Redis memory management

Source: Internet
Author: User
Tags openssl

Tcmalloc (thread-caching malloc) has the same functionality as malloc implementations of the standard GLIBC library, but the tcmalloc is much higher in efficiency and speed than standard malloc. Tcmalloc is one of the Google-perftools tools (Gperftools four tools: Tcmalloc, Heap-checker, Heap-profiler, and Cpu-profiler), the tool is open source, Published in the form of source code. If you feel you have trouble maintaining a memory allocator, consider connecting the Tcmalloc static library to your program. It is identical to the malloc invocation in glibc. All you need to do is connect the Tcmalloc dynamic library or the static library into your program, and you'll get an efficient, fast and secure memory allocator.

Compared with the standard glibc library malloc, tcmalloc in memory allocation efficiency and speed, can be high concurrency in the case of a good control of memory usage, improve server performance, reduce load. Tcmalloc implementation principles and test reports see an article: "Tcmalloc: malloc for thread Caching"

Tcmalloc as an option has been added to the "LNMP latest source one-click installation Package"

To install the Libunwind library:
If the system is 64-bit, you need to install the Libunwind library first, and the 32-bit system does not need to be installed.

The Libunwind library provides basic stack-and-Kaixie functionality for 64-bit CPUs and operating system-based programs, including APIs for output stack traces to programmatically kaixie stacks and APIs to support C + + exception handling mechanisms.

Tar XF libunwind-1.1.tar.gzcd libunwind-1.1cflags=-fpic/configuremake cflags=-fpicmake CFLAGS=-fPIC Install


Installation of Gperftools:

Tar xzf gperftools-2.1.tar.gzcd gperftools-2.1

you can add parameters to only compile Tcmalloc (./configure–enable-minimal, –disable-cpu-profiler, –disable-heap-profiler,- Disable-heap-checker, –disable-debugalloc), 64-bit operating system does not install Libunwind will not error, note that the generated library file is libtcmalloc_minimal.*
64-bit operating system, as follows

./configure

32-bit system, do not need to install Libunwind, but be sure to add –enable-frame-pointers parameters, as follows

./configure--enable-frame-pointers
Make && make install

After compiling the installation, enter the following command:

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


nginx

tar xf nginx-1.4.7.tar.gzcd nginx-1.4.7./configure  > --user=nginx > --group=nginx > --prefix=/usr/local/nginx > -- With-pcre=. /pcre-8.33 > --with-zlib=. /zlib-1.2.8 > --with-openssl=. /openssl-1.0.0l > --with-http_stub_status_module > --error-log-path=/var/log/nginx/ error.log > --http-log-path=/var/log/nginx/access.log > --pid-path=/var/run/nginx/ Nginx.pid  > --lock-path=/var/lock/nginx.lock > --with-http_gzip_static_module  > --http-proxy-temp-path=/var/tmp/nginx/proxy/ > --http-fastcgi-temp-path=/var/tmp/ nginx/fcgi/ > --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi > --http-scgi-temp-path=/ Var/tmp/nginx/scgi > --with-google_perftools_modulemake && make install 

To add a thread directory:

Mkdir/tmp/tcmallocchown-r www.www/tmp/tcmallocchmod 0777/tmp/tcmalloc vi/usr/local/nginx/conf/nginx.conf # PID next line add Google_perftools_profiles/tmp/tcmalloc;

Restart Nginx Service

/etc/init.d/nginx restart

Verify that the Tcmalloc is Nginx in effect

Yum-y Install Lsoflsof-n | grep tcmalloc

Each thread (the value of work_processes) has a row of records. The numeric value behind each thread file is the Nginx PID value that is started.




Using Tcmalloc to optimize MySQL
MySQL 5.1 static Compilation method,./configure pre-compile assumes the following parameters

--with-mysqld-ldflags=-ltcmalloc

MySQL 5.5 Static Compilation Method , cmake The following parameters when precompiled

-dcmake_exe_linker_flags= "-ltcmalloc"-dwith_safemalloc=off

Using dynamic loading

Sed-i ' [email protected] [email protected] mysqld_safe\nexport ld_preload=/usr/local/lib/[email protected] '/usr/local /mysql/bin/mysqld_safeservice mysqld Restart

Verify that the load Tcmalloc is active in MySQL as follows:

Lsof-n | grep tcmalloc


using Tcmalloc excellent Redis
Note:redis-over 2.4 comes with jemalloc , you don't need to add any parameters, through the ZMALLOC.C source we can see, redis at compile time, will first determine whether to use Tcmalloc, if it is, will use tcmalloc corresponding function to replace the standard libc in the function implementation. The second is to determine whether Jemalloc makes, and finally if not used, the standard LIBC memory management functions. So use Tcmalloc optimization carefully, these two splitter fragmentation rate is not small, it is recommended to bring the Jemalloc

Make Use_tcmalloc=yes FORCE_LIBC_MALLOC=YES/BIN/CP src/{redis-benchmark,redis-check-aof,redis-check-dump, Redis-cli,redis-sentinel,redis-server}/usr/local/redis/bin


This article is from the "Zheng" blog, make sure to keep this source http://467754239.blog.51cto.com/4878013/1558266

Tcmalloc optimized Nginx, MySQL, Redis memory management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.