Search

Dark theme | Light theme

February 7, 2025

Helidon SE Helpings: Configure Memory Health Check

You can configure a memory health check in Helidon SE. A memory health check will return a status of UP if the memory usage is below a certain threshold percentage and DOWN if the memory usage is above the threshold percentage. The default threshold percentage is 98%. To add the memory health check you need to add the dependency io.helidon.health:helidon-health-checks to your pom.xml file. This dependency contains three health checks: disk space usage, memory usage and dead lock detection.

To configure the memory health check you need to set the configuration property server.features.observe.observers.health.helidon.health.memory.thresholdPercent to the threshold percentage. Alternatively you can set the threshold percentage in your application code.

In the following example application you add a memory usage health check. First you add the dependency to the pom.xml file:

<dependency>
  <groupId>io.helidon.health</groupId>
  <artifactId>helidon-health-checks</artifactId>
</dependency>

To configure the memory health check you can add the property server.features.observe.observers.health.helidon.health.memory.thresholdPercent to the application.properties file.

...
# Set threshold percentage for memory usage.
# When the memory usage is above this percentage, the health check will fail.
# Default is 98.0
server.features.observe.observers.health.helidon.health.memory.thresholdPercent=90.0
...

In your application code you need to use the configuration to create the WebServer instance:

import io.helidon.webserver.WebServer;
import io.helidon.config.Config;
...
  public static void main(String[] args) {
    ...
    Config config = Config.create();

    WebServer server =
        WebServer.builder()
            ...
            // Read configuration properties.
            .config(config.get("server"))
            .build()
            .start();
  }
...

Instead of setting the threshold percentage in the configuration file you can also set it in your application code:

import io.helidon.health.checks.MemoryHealthCheck;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.observe.ObserveFeature;
import io.helidon.webserver.observe.health.HealthObserver;
...
  public static void main(String[] args) {
    ...
    // Configure memory usage health check.
    HeapMemoryHealthCheck heapMemoryHealthCheck =
        HeapMemoryHealthCheck.builder().thresholdPercent(90.0).build();

    // Create observe feature with health check.
    HealthObserver healthObserver =
        HealthObserver.builder()
            // Show details in health endpoint.
            .details(true)
            // Disable auto-discovery of health checks.
            .useSystemServices(false)
            // Add heap memory health check.
            .addCheck(heapMemoryHealthCheck)
            .build();

    WebServer server =
        WebServer.builder()
            .port(8080)
            // Add observe feature with health check.
            .addFeature(ObserveFeature.create(healthObserver))
            .build()
            .start();
  }
...

Once you start the application you can access the health endpoint at http://localhost:8080/health:

$ curl --silent -X GET --location "http://localhost:8080/observe/health" | jq -r .
{
  "status": "UP",
  "checks": [
    {
      "name": "heapMemory",
      "status": "UP",
      "data": {
        "free": "39.25 MB",
        "freeBytes": 41160992,
        "max": "50.00 MB",
        "maxBytes": 52428800,
        "percentFree": "78.51%",
        "total": "50.00 MB",
        "totalBytes": 52428800
      }
    }
  ]
}

Written with Helidon SE 4.1.6.