Skip to content

JVM Heap & Garbage Collection

Every nzyme-node runs in a Java Virtual Machine (JVM). Everything a node keeps in memory lives in the heap of the JVM, and the heap has a maximum size that is set when the node starts. A heap that is too small makes the node spend its time on garbage collection and finally fail with out of memory errors. A heap that is larger than the memory its host can provide stalls the node, or the operating system terminates it.

No heap size is right for every installation. The right size depends on the number of taps, on the traffic they record and on everything else that runs on the host, for example PostgreSQL. Nzyme therefore does not judge the configured size. Every node measures how its heap behaves, and the Node Heap health indicator turns on when the measurements show that a node loses time to garbage collection, runs out of heap or waits for memory.

You can find the measurements on the details page of a node under System -> Cluster & Nodes and the indicator under System -> Health Console. Both are available to super administrators only.

Setting the Heap Size

The heap size is set with the -Xms and -Xmx parameters of NZYME_JAVA_OPTS in the /etc/default/nzyme file of each node:

NZYME_JAVA_OPTS="-Xms1g -Xmx1g -XX:-OmitStackTraceInFastThrow"
Parameter Description
-Xms The heap size the JVM starts with.
-Xmx The maximum heap size. The node cannot use more heap than this.

Set both parameters to the same value, for example -Xms4g -Xmx4g for a heap of 4 GB, and restart the node:

sudo systemctl restart nzyme

The packages come with a heap of 1 GB, which is a good starting point for a small installation. In a cluster, every node has its own setting.

The heap is not the only memory a node host needs. The JVM uses memory outside of the heap, and the operating system, its file cache and every other service on the host need memory too. PostgreSQL in particular depends on the file cache of the operating system. Leave that memory free when you increase the heap of a node that shares its host with other services.

Measurements

Every node takes three measurements once per minute and keeps them for 24 hours.

Measurement Description
Time in garbage collection pauses The share of the minute in which the JVM stopped the node to collect garbage.
Heap in use after garbage collection The lowest heap usage right after a garbage collection of the minute, as a share of the maximum heap. It is only recorded in minutes with a collection.
Time stalled waiting for memory The share of the minute in which at least one thread of the node waited for the operating system to provide memory. It requires pressure stall information.

Info

The measurements are made for the garbage collectors that the JVM selects on its own. A concurrent collector like ZGC stalls a node that runs out of heap without pausing it, so the pause time stays low with it.

The heap usage between two collections says little about the health of a node. A JVM fills its heap with data that is no longer needed until the next collection removes it, so a heap that is almost full right before a collection is normal. What counts is how much of the heap is still in use after a collection, and how much time the collections take.

Health Indicator

The Node Heap indicator of the system health console judges the last 10 minutes of every running node. Like every other indicator it generates system events that you can subscribe to.

Indicator Orange when Red when
Node Heap A node spent 10% or more of the time in garbage collection pauses, or 95% or more of its heap was in use after every garbage collection, or the node was stalled waiting for memory during 10% or more of the time, or a running node does not report its measurements. A node exceeds the garbage collection pause time and the heap usage at the same time.

Every node evaluates the indicator once a minute for all nodes of the cluster. A node that has not reported for two minutes and a deleted node are not judged.

The pause time and the time stalled waiting for memory are averages, and they are only judged when at least eight of the ten minutes have a measurement. The heap usage is only judged when a collection ran in at least three of the ten minutes. A single bad minute does not turn the indicator on.

A node that is running for more than 20 minutes and has fewer than eight measurements of the last ten minutes turns the indicator orange. A node in trouble can be too busy to write its measurements, so missing measurements are not treated as a healthy node. The log of the node shows why they are missing.

Garbage collection is judged since the last start of a node only, because a restart gives the node a new heap. A node is therefore not judged for its pause time during the first eight minutes after a start, and a finding about garbage collection or heap usage is gone within a minute after you restart the node with a new heap size. The time stalled waiting for memory is judged across restarts, because it describes the host. It also turns the indicator on for a node that is terminated and restarted every few minutes because its heap does not fit into the memory of the host. The indicator goes back to green within ten minutes after such a problem is resolved.

The heap usage threshold is 95% and not lower because the default garbage collector of the JVM keeps data that is no longer needed in the heap for as long as it has the space. A healthy heap can stay just below 90% after every collection. Above that, it is the data of the node itself that fills the heap.

The health console shows a resolution procedure while the indicator is on.

Pressure Stall Information

The time a node is stalled waiting for memory comes from the pressure stall information (PSI) of the Linux kernel. It shows a heap that its host cannot provide the memory for. Garbage collection does not show this, because a node that waits for memory does not collect garbage either.

A node reads the pressure of its own control group, which only counts the node itself. It falls back to the pressure of the whole host in /proc/pressure/memory where it has no control group of its own. The pressure of the host includes every other service and container on it.

Pressure stall information is a feature of the kernel, and whether it is enabled depends on the kernel of your distribution. It is available if the following command prints two lines of numbers:

cat /proc/pressure/memory

Kernels that are built with PSI but have it switched off need the kernel parameter psi=1 at boot.

A node on a host without pressure stall information does not take this measurement. It logs this once at startup, the node details page shows no measurements for it, and the indicator judges the node by its garbage collection alone.

Info

Pressure stall information measures the time a node waits for memory. It does not measure a node that takes memory from other services. A heap that leaves PostgreSQL too little memory for the file cache can show in the DB Performance indicator, see Database Health & Maintenance.

Nodes Overview and Node Details

The nodes overview under System -> Cluster & Nodes marks a node with a warning sign next to its heap usage while the indicator is on because of that node. The sign is red if the node alone would turn the indicator red.

The details page of such a node shows a warning with the values the indicator judged, or says that the node does not report its measurements. The JVM Heap and Garbage Collection card of every node details page shows the measurements of the last 24 hours in steps of 15 minutes:

Chart Description
Heap in Use The range the heap usage moved in and the lowest heap usage after garbage collection, both as a share of the maximum heap. The indicator judges the second one only.
Time in Garbage Collection Pauses The share of time the node was stopped for garbage collection, as the average and as the range of the single minutes.
Time Stalled Waiting for Memory The share of time the node was stalled waiting for memory, as the average and as the range of the single minutes.

A dashed line in each chart marks the value at which the indicator turns on, and the value above each chart is what the indicator judges right now.

Working With the Measurements

A node with a heap that fits spends a few percent of its time in garbage collection pauses at most and is not stalled waiting for memory. Look at the charts after you connected new taps or enabled more data collection, and before the indicator turns on.

Increase the heap when the pause time is high or when the heap usage after garbage collection is at 95% or above, and the node is not stalled waiting for memory. The heap is too small for the data the node has to keep. A heap usage after garbage collection that climbs over hours or days tells you this in advance.

Do not increase the heap of a node that is stalled waiting for memory. Its host cannot provide the memory the node already uses. Reduce the heap as far as the heap usage after garbage collection allows, or add memory to the host. A node that uses 20% of its heap after a collection works with a much smaller heap, and the memory it gives back helps every other service on the host.

A heap usage after garbage collection between 80% and 90% with a low pause time needs no action. Watch the trend instead of the value.

A node with a large heap and little load collects garbage rarely. Its chart shows few values for the heap usage after garbage collection, and the indicator does not judge the heap usage in that time.