Use jemalloc with OpenSwoole

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

It is not recommended to use jemalloc, it may reduce the stability of your program, but it is supported. It is recommended to use the standard malloc and the official PHP emalloc (default).

About jemalloc

jemalloc is a more efficient memory pool technology than glibc malloc. It is widely used by Facebook, and is also used as the default memory manager in FreeBSD and Firefox projects. Using jemalloc can help improve the memory management performance of your program and may reduce memory fragmentation.

Installing jemalloc

Official GitHub: https://github.com/jemalloc/jemalloc

Follow the download instructions and compile, then install:

cd jemalloc
./configure --with-jemalloc-prefix=je_
make -j 4

Using jemalloc

After installing jemalloc, you can configure it to work with OpenSwoole:

phpize
./configure --with-jemalloc-dir=/path/to/jemalloc
make
make install

Using different implementations

You can also use different memory pool implementations by using LD_PRELOAD, for example, we can also use Google's tcmalloc:

LD_PRELOAD="/usr/lib/libjemalloc.so" php server.php
LD_PRELOAD="/usr/lib/libtcmalloc.so" php server.php
Last updated on October 14, 2022