首页 > 解决方案 > 使用 AWS Lambda C++ 运行时通过 AWS Lambda 读取 S3 对象时发生内存泄漏

问题描述

我正在使用 AWS Lambda C++ 运行时读取带有 AWS Lambda 的 S3 对象。我使用这个功能:

void s3read(const std::string& bucket, const std::string& key, const std::string& filename_local) {
    Aws::Client::ClientConfiguration client_conf;
    client_conf.region = Aws::Environment::GetEnv("AWS_REGION");
    client_conf.caFile = "/etc/pki/tls/certs/ca-bundle.crt";
    Aws::S3::S3Client s3_client(client_conf);
    Aws::S3::Model::GetObjectRequest object_request;
    object_request.WithBucket(bucket.c_str()).WithKey(key.c_str());
    auto get_object_outcome = s3_client.GetObject(object_request);
    if(get_object_outcome.IsSuccess()) {
        auto &retrieved_file = get_object_outcome.GetResultWithOwnership().GetBody();
        Aws::OFStream local_file;
        local_file.open(filename_local.c_str(), std::ios::in | std::ios::out | std::ios::binary);
        local_file << retrieved_file.rdbuf(); // Leak
        local_file.close();
    };
    std::remove(filename_local.c_str()); // For leak testing purposes
};

它工作正常。但是,如果我反复调用 Lambda, Max Memory Used会不断增长。泄漏似乎来自rdbuf(),但我不知道如何解决它。

更新:当filename_local/dev/null 时没有泄漏。

标签: c++amazon-web-servicesaws-lambdaaws-sdk-cpp

解决方案


推荐阅读