python - 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())
解决方案
推荐阅读
- javascript - 从姊妹文件 Node.JS 导入函数
- python - Scapy 的 RSSI 信息
- android - Firebase 是否可以从 Android 应用程序的 html 页面注销?
- ruby-on-rails - rails方法:删除不起作用
- php - Php递归函数无限循环错误
- c# - 使用 Reference 或 Collection 方法访问导航属性
- c - 将库中的专用 ELF 部分合并到应用程序专用 ELF 部分
- android - zxing android 二维码生成器
- c# - 编写 if 语句是否会影响程序的速度和效率?
- flutter - Flutter中输入值时如何保存TextField的值