[英]Memory Space issue while adding more images for generating word document using docx node library
const imageResponse = await axios.get(url[0], {
responseType: "arraybuffer",
});
const buffer = Buffer.from(imageResponse.data, "utf-8");
const image = Media.addImage(doc, buffer);
I'm using the above code inside one loop that will execute 100 times because it has 100 images.我在一个循环中使用上述代码,该循环将执行 100 次,因为它有 100 张图像。 Each image size is max 150kb.
每个图像大小最大为 150kb。 I deployed the cloud function with 256mb.
我用 256mb 部署了云功能。 I'm getting "Error: memory limit exceeded. Function invocation was interrupted".
我收到“错误:超出内存限制。函数调用被中断”。
Problem statement:问题陈述:
I need to add 250 images in word document.我需要在 word 文档中添加 250 张图片。 I'm getting memory limit exceeded error.
我收到超出内存限制的错误。
Q&A问答
Is there any way to get one image and add to word document, after that clearing the memory used by the image?在清除图像使用的内存之后,有没有办法获取一张图像并添加到word文档中?
How to effectively use this plugin in firebase cloud function with cloud storage for images?如何在带有云存储图像的 firebase 云功能中有效地使用此插件?
Environment:环境:
Firebase Cloud Function (NodeJs) Firebase 云函数 (NodeJs)
Size : 256mb大小:256mb
Word Doc Generating Library : docx ( https://docx.js.org/#/ ) Word Doc 生成库:docx ( https://docx.js.org/#/ )
For the kind of scenario you are describing, as Doug mentions, you should consider increasing your resources to better handle the requests to your functions.对于您所描述的那种场景,正如 Doug 所提到的,您应该考虑增加资源以更好地处理对您的功能的请求。
You can set the memory using the flag memory using the gcloud command available for deploy your functions, for example:您可以使用可用于部署函数的 gcloud 命令使用标志内存设置内存,例如:
gcloud beta functions deploy my_function --runtime=python37 --trigger-event=providers/cloud.firestore/eventTypes/document.write --trigger-resource=projects/project_id/databases/(default)/documents/messages/{pushId}
--memory=AmountOfMemory
I recommend you take a look at the best practices for cloud functions document where is explained:我建议您查看云功能文档的最佳实践,其中解释了:
"Local disk storage in the temporary directory is an in-memory filesystem. Files that you write consume memory available to your function, and sometimes persist between invocations. Failing to explicitly delete these files may eventually lead to an out-of-memory error and a subsequent cold start."
“临时目录中的本地磁盘存储是内存文件系统。您写入的文件会消耗函数可用的内存,并且有时会在调用之间持续存在。未能明确删除这些文件可能最终会导致内存不足错误和随后的冷启动。”
For have a better perspective about how Cloud functions manage the requests, check this document where is mentioned:为了更好地了解云功能如何管理请求,请查看此文档,其中提到:
"Cloud Functions handles incoming requests by assigning them to instances of your function. Depending on the volume of requests, as well as the number of existing function instances, Cloud Functions may assign a request to an existing instance or create a new one
“Cloud Functions 通过将传入请求分配给您的函数实例来处理传入请求。根据请求量以及现有函数实例的数量,Cloud Functions 可能会将请求分配给现有实例或创建一个新实例
Each instance of a function handles only one concurrent request at a time.
函数的每个实例一次仅处理一个并发请求。 This means that while your code is processing one request, there is no possibility of a second request being routed to the same instance.
这意味着当您的代码正在处理一个请求时,不可能将第二个请求路由到同一个实例。 Thus the original request can use the full amount of resources (CPU and memory) that you requested."
因此,原始请求可以使用您请求的全部资源(CPU 和内存)。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.