使用 Vert.x & Netty 下载大文件时如何处理 运行 直接内存不足?

How to handle running out of direct memory when downloading large files with Vert.x & Netty?

我有一个 Vert.x Web 服务,偶尔会从 AWS S3 下载大型 ZIP 文件。下载后,解压缩存档并将各个文件重新上传到 AWS S3。 Web 服务作为 t2.large(8GB 内存)实例托管在 AWS Elastic Beanstalk 中。 Java 应用程序当前配置了 2-4GB 的堆 space,ZIP 文件的大小最多为 10GB(但大多数最多接近 2-4GB)。

当应用程序尝试下载大小超过 2GB 的 ZIP 文件时,ZIP 文件的初始下载或单个文件的重新上传总是失败,堆栈跟踪类似于以下内容:

Caused by: io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 16777216 byte(s) of direct memory (used: 1895825439, max: 1908932608)

经过一些研究,Vert.x 似乎使用 Netty 来加速网络 I/O,后者又利用直接内存来提高下载性能。似乎直接内存没有被足够快地释放,这导致了像上面这样的内存不足异常。

最简单的解决方案就是将实例大小增加到 16GB t2.xlarge 并在运行时分配更多直接内存(例如 -XX:MaxDirectMemorySize),但我想先探索其他解决方案.有没有办法以编程方式强制 Netty 在不再使用后释放直接内存?我可以添加额外的 Vert.x 配置来缓解这个问题吗?

请检查这个

github.com/aws/aws-sdk-java-v2/issues/1301

we have identified an issue within the SDK where it could cause excessive buffer usage and eventually OOM when using s3 async client to download a large object to a file. The fix #1335 is available in 2.7.4. Could you try with the latest version? Feel free to re-open if you continue to see the issue. " – AWS PS 21 hours ago Delete