static ExitCode CheckTotalMemoryLimit()

in CodeSnippets/MemoryUsage/MemoryOveruseDetectionAndCleanup/memory_overuse_detection_and_cleanup.c [25:53]


static ExitCode CheckTotalMemoryLimit(void)
{
    // Depending on the logic of the application, the call to the
    // Applications_GetTotalMemoryUsageInKB may be replaced with any of the functions described here
    // https://learn.microsoft.com/azure-sphere/app-development/application-memory-usage?pivots=visual-studio#determine-run-time-application-ram-usage
    size_t totalMemoryUsage = Applications_GetTotalMemoryUsageInKB();

    if (totalMemoryUsage == 0) {
        Log_Debug("ERROR: Applications_GetTotalMemoryUsageInKB failed: %s (%d)\n", strerror(errno),
                  errno);

        // User defined: https://learn.microsoft.com/azure-sphere/app-development/exit-codes
        return ExitCode_CheckTotalMemoryLimit_GetTotalMemoryUsageInKB_Failed;
    }

    // To aid debugging, telemetry may be sent to the cloud with the memory usage details from
    // the memory APIs. For general illustration of how to send telemetry check the AzureIoT sample:
    // https://github.com/Azure/azure-sphere-samples/blob/main/Samples/AzureIoT/main.c
    // e.g SendTelemetry("{\"TotalMemoryUsed\" : totalMemoryUsage}");
    if (totalMemoryUsage >= TotalMemoryLimit) {
        Log_Debug("ERROR: TotalMemoryUsed reached: %zu KB\n", totalMemoryUsage);

        // User defined: https://learn.microsoft.com/azure-sphere/app-development/exit-codes
        return ExitCode_CheckTotalMemoryLimit_Overflow;
    }

    // User defined: https://learn.microsoft.com/azure-sphere/app-development/exit-codes
    return ExitCode_Success;
}