jemalloc.allocated, jemalloc.active and jemalloc.resident. If allocated is high, the server really holds that much data, and you have to find the component that owns it. If allocated is small and active is large, the memory is lost to fragmentation inside the allocator: no component owns it, and SYSTEM JEMALLOC PURGE cannot return it.
Before you start
Rule out the usual consumers first: running queries, merges, dictionaries, primary keys, caches and in-memory tables. Debugging memory issues has a query for each of them. This article is for the case when all of them are small, but theMemoryTracking metric stays high, or queries fail with (total) memory limit exceeded while the failed allocation is small and current RSS in the message is close to maximum. ClickHouse corrects MemoryTracking from the measured memory usage of the process, and the limit of the server is checked against the resident memory as well. Memory that the allocator holds but does not use therefore counts against max_server_memory_usage like live data.
Read the three numbers
Each server has its own allocator, so on a cluster read the numbers of every replica separately. Query all replicas with the
clusterAllReplicas table function and group by the server. Without the GROUP BY, the result is one row that mixes values of different servers:
default: FROM clusterAllReplicas(default, system.asynchronous_metrics).
Look at the trend, not at one reading
One reading proves little: after a burst of queries the gap betweenactive and allocated is large for a while on a healthy server. system.asynchronous_metric_log keeps the history. A low quantile per day approximates the level at rest between bursts:
allocatedgrows together withactive: the working set grows. Go back to the per-component checks, or profile the allocations.allocatedstays flat whileactiveclimbs day after day: fragmentation accumulates.residentis far aboveactive: freed pages are waiting to be returned. This is normally temporary.
Example
In a test, 3000 empty tables of theMemory engine with 31 columns each were created, and then 2700 of them were dropped with DROP TABLE ... SYNC. The metadata of a table consists of many small allocations, and the objects of the tables that remain are spread over the same slabs as the objects of the dropped ones.
Dropping the tables brought
allocated back close to where it started, but active did not go down, and the purge returned only the few dirty pages.
Check what a purge can return
SYSTEM JEMALLOC PURGE returns unused dirty pages to the operating system. It cannot return a page that still contains a live allocation. Before you run or schedule it, check how many dirty pages there are:
pactive, pdirty and pmuzzy are numbers of pages, not bytes, so compare them as a ratio. If pdirty is a small fraction of pactive, a purge returns next to nothing.
The server already purges on its own. Background threads of the allocator return dirty pages within dirty_decay_ms milliseconds (5000 by default), and the memory worker forces a purge when the memory usage or the amount of dirty pages is high (see memory_worker_purge_total_memory_threshold_ratio and the related settings). Under sustained memory pressure, and on machines with less than 4 GiB of memory, the memory worker sets dirty_decay_ms to 0, so that pages are returned as soon as they are freed. The query above shows the current value.
Find the size classes that waste memory
system.jemalloc_bins shows the fragmentation per size class. Reading it requires the SYSTEM JEMALLOC privilege.
size is the size of an allocation in the class, curslabs the number of slabs that the class holds, and curregs the number of live allocations. The column util is the share of the regions in those slabs that are in use, and waste is the number of bytes in the slabs that no live allocation uses. A slab is released only when every region in it is free, so a few long-lived objects can keep many nearly empty slabs. The statistics are approximate because of the caches that the allocator keeps per thread: use them to rank the size classes, not as exact totals.
Waste that is concentrated in small size classes points to a workload that creates very many small objects of which some live for a long time. To find the code that allocates them, follow Analyzing fragmentation from SQL (version 26.9 and later). A regular heap profile ranks call stacks by live bytes: it explains allocated, not the gap between allocated and active.
What you can do about fragmentation
- Restart the server to release the memory at once. The heap starts empty, and the fragmentation builds up again at the rate that the workload produces it, so this is a mitigation, not a fix.
- Change the workload that causes it, once the fragmentation analysis has shown which allocations stay alive.
- Leave headroom. Size the memory of the server for the
residentlevel that you observe, not forallocated. - Upgrade. Newer releases change how memory is allocated; for example, version 26.9 changed an allocator option to reduce fragmentation.
- If the growth does not stop, report it with the outputs of the queries in this article taken at several points in time.