esp32 http请求内存不断减少问题

一次esp32 http请求内存不断减少问题的解决过程

现象

从网上复制了一段http请求的代码,功能是通的,但发现每请求一次,内存就减少10多kb,很快就内存不足重启了。
一开始以为是cjson解释动作时没有注意释放,但检查并不是,最后参考官方的http client例程,才发现复制的代码最后只是简单使用了esp_http_client_close(client),并没有使用官方例程的esp_http_client_cleanup(client)方法。
esp_http_client_cleanup方法其实先调用了esp_http_client_close,然后再对所有相关缓存使用了free()释放。
所以以后只要在任务或者是定时里面不断调用http client的话,应该使用esp_http_client_cleanup方法。
在此记录一下

static void http_redirect_to_https(void)
{
    
    
    esp_http_client_config_t config = {
    
    
        .url = "http://httpbin.org/redirect-to?url=https%3A%2F%2Fwww.howsmyssl.com",
        .event_handler = _http_event_handler,
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);
    esp_err_t err = esp_http_client_perform(client);

    if (err == ESP_OK) {
    
    
        ESP_LOGI(TAG, "HTTP redirect to HTTPS Status = %d, content_length = %lld",
                esp_http_client_get_status_code(client),
                esp_http_client_get_content_length(client));
    } else {
    
    
        ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
    }
    esp_http_client_cleanup(client);
}

猜你喜欢

转载自blog.csdn.net/flamebox/article/details/122280431