arrow-left

All pages
gitbookPowered by GitBook
1 of 3

Loading...

Loading...

Loading...

Specifying Which Plugins to Build

Explains how to use CMake options like `PLUGIN_xxx` to control which plugins are built statically, dynamically, or not at all during compilation.

By default all plugins are enabled and built as dynamic .so (or .dll) modules. If a plugin does not support dynamic builds, it is not built at all.

Use PLUGIN_xxx cmake variables. They can be set on the command line with -DPLUGIN_xxx=value or in the cmake gui. Supported values are

Value
Effect

Note that unlike autotools, cmake tries to configure and build incrementally. You can modify one configuration option and cmake will only rebuild the part of the tree affected by it. For example, when you do cmake -DWITH_EMBEDDED_SERVER=1 in the already-built tree, it will make libmysqld to be built, but no other configuration options will be changed or reset to their default values.

In particular this means that if you have run, for examplecmake -DPLUGIN_OQGRAPH=NO and later you want to restore the default behavior (with OQGraph being built) in the same build tree, you would need to runcmake -DPLUGIN_OQGRAPH=DYNAMIC

Alternatively, you might simply delete the CMakeCache.txt file — this is the file where cmake stores current build configuration — and rebuild everything from scratch.

This page is licensed: CC BY-SA / Gnu FDL

NO

the plugin will be not compiled at all

STATIC

the plugin will be compiled statically, if supported. Otherwise it will be not compiled.

DYNAMIC

the plugin will be compiled dynamically, if supported. Otherwise it will be not compiled. This is the default behavior.

AUTO

the plugin will be compiled statically, if supported. Otherwise it will be compiled dynamically.

YES

same as AUTO, but if plugin prerequisites (for example, specific libraries) are missing, it will not be skipped, it will abort cmake with an error.

Compiling MariaDB with Extra Modules/Options

Compile MariaDB Server with extra modules and options. This section details how to customize your build from source, enabling specific features or optimizations for your deployment.

Using MariaDB with TCMalloc or jemalloc

Instructions on building and configuring MariaDB to use alternative memory allocators like TCMalloc or jemalloc for improved performance and profiling.

circle-info

Read the page for more information on how to debug high memory consumption.

hashtag
Using tcmalloc or jemalloc

is a malloc replacement library optimized for multi-threaded usage. It features a built-in heap debugger and profiler. Another malloc replacement that can speed up MariaDB is .

The procedures to use one of these libraries with MariaDB are the same. Many other malloc replacement libraries (as well as heap debuggers and profilers) can be used with MariaDB in a similar fashion.

hashtag
Prerequisites

jemalloc is a powerhouse for reducing memory fragmentation and improving concurrency, but it isn't a "one-size-fits-all" drop-in for every environment. Its availability and stability depend heavily on how the operating system handles memory pages and how MariaDB was compiled.

hashtag
Operating System Availability

  • Linux: Jemalloc is most at home here. Most major distributions (Ubuntu, Debian, CentOS/RHEL, Arch) include it in their official repositories (libjemalloc2 or jemalloc). It is the standard recommendation for high-traffic MariaDB setups on Linux.

  • Windows: Jemalloc does not have the same "native" presence on Windows. While the jemalloc source code can technically be compiled for Windows using MSVC or MinGW, MariaDB for Windows is almost exclusively used with the default Windows system allocator. Most DBAs do not use jemalloc on Windows production servers.

hashtag
Hardware and Architecture Hurdles

One of the most common "hidden" failures occurs on non-x86 architectures:

  • Raspberry Pi (ARM): On newer devices like the Raspberry Pi 5, jemalloc often fails to start with the error: <jemalloc>: Unsupported system page size. This happens because jemalloc is frequently compiled assuming a 4KB page size, while many ARM kernels now use 16KB or 64KB pages for better performance.

  • Cloud Instances: Small instances (like AWS t2.micro) can sometimes behave unpredictably with jemalloc if they lack sufficient swap space or if the memory overhead of the

hashtag
Compatibility Matrix Summary

hashtag
Check if it's Ready to Use

Before trying to force it via LD_PRELOAD, check if the library even exists on your system paths:

If these commands return nothing, you need to install the package first (sudo apt install libjemalloc2 or sudo dnf install jemalloc).

hashtag
Why Use it

The main reason to switch isn't just speed, but fragmentation. Standard malloc can leave small gaps of unused memory that the OS can't reclaim, leading to "memory bloat" where MariaDB appears to use 10GB of RAM even if the data only needs 6GB. Jemalloc is much better at packing these allocations tightly.

hashtag
Checking the malloc Implementation in Use

To check which malloc implementation is used, run this query:

A value of system indicates the system default, which is normally malloc. If another library is used, the name and version of the library is shown.

hashtag
Building MariaDB With an Alternative to malloc

To build MariaDB with TCMalloc, you need to use the following command:

To use jemalloc, specify -ljemalloc instead of -ltcmalloc.

hashtag
Starting mariadbd-safe With an Alternative to malloc

Start a standard MariaDB server with TCmalloc like this:

To configure to use tcmalloc or jemalloc, add this to your :

hashtag
Starting mariadbd With an Alternative to malloc

Locate the library file that needs to be used:

Pass that library file to mariadbd using the LD_PRELOAD variable:

hashtag
Configuring systemd

If you use systemd to run MariaDB, locate the library as explained above, then locate the service configuration file:

Edit the mariadb.service file by adding a line to the [Service] group:

For example, add this:

Reload the configuration for the news setting to take effect, and restart MariaDB:

hashtag
Dockerfile

If you run and use an image from a Dockerfile that is publicly available, most probably you have an entry point that is a bash script, which starts mariadbd directly. Edit that bash script, or set the LD_PRELOAD variable from the Dockerfile:

To find the library file, run one of these commands while the container is running:

Example:

hashtag
Vagrantfile

Usually is used to start a complete system in a virtual machine. In that case, you can use one of the methods above, for example you can modify your Vagrantfile to copy a modified version of the mariadb.service file to the guest system to configure systemd.

If you use Vagrant with the Docker provider, you can follow the instructions above to modify the Dockerfile.

hashtag
Making jemalloc Persistent

To make MariaDB use jemalloc persistently, set it up like this. These instructions work on Linux using systemd and running on an x86 architecture.

hashtag
Steps

1

hashtag
Identify the jemalloc library path.

Identify the jemalloc library path, to find the exact location of the jemalloc shared library on your system:

Take a note of the output path. Typically, it is something like

hashtag
Notes

systemctl edit creates a separate configuration file (/etc/systemd/system/mariadb.service.d/override.conf). systemd always prioritizes settings in override files over the main service file (/usr/lib/systemd/system/mariadb.service).

When upgrading MariaDB, the package manager only replaces or updates the main service file. Your custom override file in /etc/systemd/system/mariadb.service.d/ remains untouched, thus preserving your jemalloc configuration.

systemd reads all its service configurations, including override files, on system start. This means the LD_PRELOAD environment variable is set for MariaDB automatically each time the service starts, making the change persistent across reboots.

hashtag
Finding memory leaks with jemalloc

jemalloc provides a report of memory leaks at program exit:

This produces output like this:

You can learn more about the memory leaks with jeprof, which is shipped with jemalloc:

You can also generate a PDF call graph of the leak:

hashtag
See Also

This page is licensed: CC BY-SA / Gnu FDL

FreeBSD: This is the "birthplace" of jemalloc – it replaced the system malloc in FreeBSD back in 2005. If you are running MariaDB on FreeBSD, you are likely using jemalloc by default at the OS level.

jemalloc
metadata conflicts with the tiny RAM footprint.

Experimental

Possible but lacks the robust testing seen on Linux.

/usr/lib64/libjemalloc.so.2
.
2

hashtag
Create or edit the MariaDB Systemd override file.

This is the crucial step for persistence. Use systemctl edit to manage the override file:

If an override file already exists for MariaDB, it opens in the editor. If not, the editor creates a new, empty file, typically at /etc/systemd/system/mariadb.service.d/override.conf. Add the following content to the file:

Replace /usr/lib64/libjemalloc.so.2 with the actual path you noted in step 1. Save the file and exit the editor.

3

hashtag
Restart services and MariaDB.

4

hashtag
Reboot the computer, and verify that jemalloc is used.

You can verify this on various levels.

MariaDB:

Operating system:

Operating system processes:

Platform

Jemalloc Support

Typical Usage

Linux (x86_64)

Excellent

Highly recommended; installed via apt or yum.

FreeBSD

Native

It is the system default.

Windows

Poor/Partial

Rarely used; usually requires custom builds.

ARM (RPi/M1/M2)

Conditional

Depends on the kernel page size (4KB vs 16KB).

TCMallocarrow-up-right
jemallocarrow-up-right
mariadbd-safe
configuration file
MariaDB on Docker
Vagrant
Jemalloc leak checkingarrow-up-right

Solaris/Illumos

sudo systemctl edit mariadb
sudo systemctl daemon-reexec 
sudo systemctl daemon-reload 
sudo systemctl restart mariadb
mariadb -e "SHOW GLOBAL VARIABLES LIKE 'version_malloc_library';"
+------------------------+------------------------------------------------------------+ 
| Variable_name          | Value                                                      | 
+------------------------+------------------------------------------------------------+ 
| version_malloc_library | jemalloc 5.2.1-0-gea6b3e973b477b8061e0076bb257dbd7f3faa756 | 
+------------------------+------------------------------------------------------------+
sudo cat /proc/$MARIADB_PID/environ | tr '\0' '\n' | grep LD_PRELOAD
LD_PRELOAD=/usr/lib64/libjemalloc.so.2
# On Debian/Ubuntu
find /usr/lib -name "libjemalloc.so.*"

# On RHEL/CentOS
find /usr/lib64 -name "libjemalloc.so.*"
SHOW GLOBAL VARIABLES LIKE 'version_malloc_library';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| version_malloc_library | system |
+------------------------+--------+
cmake -DCMAKE_EXE_LINKER_FLAGS='-ltcmalloc'  -DWITH_SAFEMALLOC=OFF
/usr/sbin/mariadbd-safe --malloc-lib=tcmalloc
[mariadbd-safe]
malloc-lib=tcmalloc
# jemalloc
find /usr/lib -name "libjemalloc.so.*"
# tcmalloc
find /usr/lib -name "libtcmalloc.so.*"
LD_PRELOAD=/path/to/library mariadbd
systemctl status mariadb | grep Loaded
[Service]
Environment=LD_PRELOAD=<path-to-library>
[Service]
Environment=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2
systemctl daemon-reload
systemctl restart mariadb
ENV LD_PRELOAD=<path-to-library>
# jemalloc
docker exec -ti <container-name> find /usr/lib -name "libjemalloc.so.*"
# tcmalloc
docker exec -ti <container-name> find /usr/lib -name "libtcmalloc.so.*"
docker run -P -d --name mariadb --env LD_PRELOAD="/usr/lib/x86_64-linux-gnu/libjemalloc.so.2" --env MARIADB_ROOT_PASSWORD=Password123! mariadb:latest
find /usr -name "libjemalloc.so.*"
MALLOC_CONF=prof_leak:true,lg_prof_sample:0,prof_final:true \
LD_PRELOAD=${JEMALLOC_PATH}/lib/libjemalloc.so.2  path-to-mariadbd
<jemalloc>: Leak summary: 267184 bytes, 473 objects, 20 contexts
<jemalloc>: Run jeprof on "jeprof.19678.0.f.heap" for leak detail
jeprof --show_bytes path-to-mariadbd jeprof.19678.0.f.heap
jeprof --show_bytes --pdf path-to-mariadbd  jeprof.19678.0.f.heap > /tmp/mariadbd.pdf
[Service] 
Environment="LD_PRELOAD=/usr/lib64/libjemalloc.so.2"
sudo cat /proc/$MARIADB_PID/maps | grep jemalloc
7f6981c00000-7f6981c06000 r--p 00000000 fd:00 50981369 /usr/lib64/libjemalloc.so.2 
7f6981c06000-7f6981c76000 r-xp 00006000 fd:00 50981369 /usr/lib64/libjemalloc.so.2 
7f6981c76000-7f6981c81000 r--p 00076000 fd:00 50981369 /usr/lib64/libjemalloc.so.2 
7f6981c81000-7f6981c87000 r--p 00080000 fd:00 50981369 /usr/lib64/libjemalloc.so.2 
7f6981c87000-7f6981c88000 rw-p 00086000 fd:00 50981369 /usr/lib64/libjemalloc.so.2
spinner
spinner
Profiling Memory Usage
Profiling memory usage