首页 > 解决方案 > Lambda 中的内存使用率极高

问题描述

我很难解决这个内存使用问题

目标的简要说明:

创建许多文件的 zip 并将其保存在 S3 上

采取的信息和步骤:

这些文件是图像,大小在 10-50mb 之间,会有数千个。

我已将 Lambda 调整为 3GB 内存,我希望每个 zip 文件有 2.5-3GB 的文件。

问题:

目前,我有一个正好是 1GB 的测试文件。问题是,Lambda 报告使用的内存大约是 2085。我知道会有开销,但为什么是 2GB?

伸展目标:

能够创建大于 3GB 的 zip

测试和结果:

如果我将 in_memory.seek(0) 和 Object.Put 注释掉到 S3,则不会发生内存加倍(但是,也不会创建任何文件......)

当前代码:

bucket = 'dev'
s3 = boto3.resource('s3')
list = [
    "test/3g/file1"
]
list_index = 1


# Setup a memory store for streaming into a zip file
in_memory = BytesIO()

# Initiate zip file with "append" settings
zf = ZipFile(in_memory, mode="a")

# Iterate over contents in S3 bucket with prefix, filtering empty keys
for key in list:
    object = s3.Object(bucket, key).get()

    # Grab the filename without the key to create a "flat" zip file
    filename = key.split('/')[-1]

    # Stream the body of each key into the zip
    zf.writestr(filename, object['Body'].read())

# Close the zip file
zf.close()

# Go to beginning of memory store
in_memory.seek(0)

# Stream zip file to S3
object = s3.Object('dev', 'test/export-3G-' + str(list_index) + '.zip')
object.put(Body=in_memory.read())

标签: pythonamazon-s3aws-lambdaaws-sdkzipfile

解决方案


推荐阅读