C SDK
Memory Configuration

Memory Configuration

The Plexus C SDK uses compile-time flags to control buffer sizes and feature toggles. You can reduce RAM usage from the default ~17 KB down to ~1.5 KB by disabling features you do not need.

Configuration Options

Override any of these defaults by defining them before including plexus.h or via compiler flags (e.g., -DPLEXUS_MAX_METRICS=8).

Buffer Sizes

FlagDefaultDescription
PLEXUS_MAX_METRICS32Max metrics per flush
PLEXUS_MAX_METRIC_NAME_LEN64Max metric name length (bytes)
PLEXUS_MAX_STRING_VALUE_LEN128Max string value length (bytes)
PLEXUS_MAX_SOURCE_ID_LEN64Max source ID length
PLEXUS_MAX_SESSION_ID_LEN64Max session ID length
PLEXUS_MAX_API_KEY_LEN128Max API key length
PLEXUS_MAX_ENDPOINT_LEN256Max endpoint URL length
PLEXUS_JSON_BUFFER_SIZE2048JSON serialization buffer

Tag Settings

FlagDefaultDescription
PLEXUS_MAX_TAG_LEN32Max tag key/value length
PLEXUS_MAX_TAGS4Max tags per metric

Network Settings

FlagDefaultDescription
PLEXUS_DEFAULT_ENDPOINT"https://app.plexus.company/api/ingest"Default ingest URL
PLEXUS_HTTP_TIMEOUT_MS10000HTTP request timeout
PLEXUS_MAX_RETRIES3Retry count on failure
PLEXUS_RETRY_BASE_MS500Initial retry delay
PLEXUS_RETRY_MAX_MS8000Maximum retry delay
PLEXUS_RATE_LIMIT_COOLDOWN_MS30000Cooldown after 429 response

Auto-Flush Settings

FlagDefaultDescription
PLEXUS_AUTO_FLUSH_COUNT16Auto-flush after N metrics
PLEXUS_AUTO_FLUSH_INTERVAL_MS5000Auto-flush interval (0 = disabled)

Feature Toggles

FlagDefaultDescription
PLEXUS_ENABLE_TAGS1Enable metric tags
PLEXUS_ENABLE_STRING_VALUES1Enable string value support
PLEXUS_ENABLE_BOOL_VALUES1Enable boolean value support
PLEXUS_ENABLE_PERSISTENT_BUFFER0Enable flash-backed buffer on flush failure
PLEXUS_ENABLE_STATUS_CALLBACK0Enable connection status notifications
PLEXUS_ENABLE_THREAD_SAFE0Enable mutex-protected client access
PLEXUS_DEBUG0Enable debug logging

RAM Usage Breakdown

The client struct (plexus_client_t) is the only RAM allocation. Its size depends on your configuration:

ComponentSize FormulaDefault
Metrics bufferPLEXUS_MAX_METRICS * sizeof(plexus_metric_t)~14.8 KB (32 × 472 B)
JSON bufferPLEXUS_JSON_BUFFER_SIZE2048 B
Fixed fieldsAPI key + source ID + session ID + endpoint + counters~512 B
Total~17 KB

The size of plexus_metric_t is 472 bytes with all features enabled (strings, bools, tags). Disabling string values removes 128 bytes per metric and disabling tags removes 257 bytes per metric — this is the main lever for reducing RAM. Use plexus_client_size() at runtime or PLEXUS_CLIENT_STATIC_SIZE at compile time to get the exact size for your configuration.

Default vs Minimal

ConfigurationApproximate RAM
Default (all features)~17 KB
Minimal (numbers only)~1.5 KB

Default Configuration

All features enabled, 32 metrics per flush:

// No flags needed — these are the defaults
plexus_client_t* px = plexus_init("plx_xxxxx", "device-001");
printf("Client size: %u bytes\n", (unsigned)plexus_client_size());
// => ~17704 bytes

Minimal Configuration

Numbers only, 8 metrics per flush, small buffers:

-DPLEXUS_MAX_METRICS=8
-DPLEXUS_ENABLE_TAGS=0
-DPLEXUS_ENABLE_STRING_VALUES=0
-DPLEXUS_ENABLE_BOOL_VALUES=0
-DPLEXUS_JSON_BUFFER_SIZE=512
-DPLEXUS_MAX_ENDPOINT_LEN=128
-DPLEXUS_MAX_API_KEY_LEN=64

This reduces plexus_client_size() to approximately 1.5 KB.

In CMake:

target_compile_definitions(your_target PRIVATE
    PLEXUS_MAX_METRICS=8
    PLEXUS_ENABLE_TAGS=0
    PLEXUS_ENABLE_STRING_VALUES=0
    PLEXUS_ENABLE_BOOL_VALUES=0
    PLEXUS_JSON_BUFFER_SIZE=512
    PLEXUS_MAX_ENDPOINT_LEN=128
    PLEXUS_MAX_API_KEY_LEN=64
)

In Arduino/PlatformIO (platformio.ini):

build_flags =
    -DPLEXUS_MAX_METRICS=8
    -DPLEXUS_ENABLE_TAGS=0
    -DPLEXUS_ENABLE_STRING_VALUES=0
    -DPLEXUS_ENABLE_BOOL_VALUES=0
    -DPLEXUS_JSON_BUFFER_SIZE=512
    -DPLEXUS_MAX_ENDPOINT_LEN=128
    -DPLEXUS_MAX_API_KEY_LEN=64

Persistent Buffering

Enable flash-backed storage so unsent data survives power cycles and reboots:

-DPLEXUS_ENABLE_PERSISTENT_BUFFER=1

When a flush fails, the SDK writes queued metrics to persistent storage (NVS on ESP32). On the next successful connection, stored data is sent first.

FlagDefaultDescription
PLEXUS_PERSIST_MAX_BATCHES8Number of batch slots in the persistent ring buffer

The persistent buffer HAL must be implemented for your platform. The ESP32 HAL uses NVS (nvs_flash). For STM32, you would implement plexus_hal_storage_write/read/clear using your flash storage driver.

Thread Safety

Enable mutex-protected client access for multi-threaded environments:

-DPLEXUS_ENABLE_THREAD_SAFE=1

When enabled, all public API calls acquire a mutex before accessing client state. The mutex HAL must be implemented for your platform:

PlatformImplementation
ESP32 (FreeRTOS)xSemaphoreCreateRecursiveMutex()
STM32 (FreeRTOS)osMutexCreate()
STM32 (bare-metal)No-op stubs

Without PLEXUS_ENABLE_THREAD_SAFE, the SDK is not thread-safe. Confine all calls for a given client to a single thread. Multiple clients in separate threads is always safe.

Blocking Behavior

plexus_flush() can block during retries. Here is the worst-case timing:

AttemptDelayCumulative
1st try0 ms0 ms
2nd try (retry 1)500 ms~500 ms
3rd try (retry 2)1000 ms~1500 ms
Give up~1.5 s + HTTP timeouts (with jitter)
PlatformBehavior During Delay
FreeRTOS (ESP32/STM32)vTaskDelay() / osDelay() — other tasks continue
Bare-metal (Arduino)delay() — blocks the entire MCU
Bare-metal (STM32)HAL_Delay() — blocks the entire MCU

To avoid blocking your main loop, reduce PLEXUS_MAX_RETRIES or use FreeRTOS with a dedicated telemetry task.

Next Steps