如何將 Java 堆積傾印檔案匯出到 S3 儲存貯體?
我在 Amazon Elastic Container Service (Amazon ECS) 中儲存的 Java 應用程式中遇到了 "heap out of memory" 錯誤。為了對錯誤進行疑難排解,我想建立一個 .hprof 檔案,並將其保存在 Amazon Simple Storage Service (Amazon S3) 儲存貯體中。
簡短描述
您最常遇到 OutOfMemoryError 例外狀況的原因是,Java 虛擬機器 (JVM) 無法為堆積分配足夠的記憶體。當 Java 應用程式在 ECS 容器中執行時,"heap out of memory" 錯誤會導致應用程式中斷,並停止 ECS 任務。應用程式儲存的任何 .hprof 檔案都無法再使用。若要對例外狀況錯誤進行疑難排解,並偵錯記憶體分配問題,您必須建立 .hprof 檔案,並將其儲存到 S3 儲存貯體。
解決方法
**注意:**以下解決方案可解決大部分 OutOfMemoryError 例外狀況。如需了解 Java 記憶體錯誤成因的更多資訊,請參閱 Oracle 網站上的了解 OutOfMemoryError 例外狀況。
建立 S3 儲存貯體
使用 Amazon S3 主控台建立 S3 儲存貯體,儲存您的 .hprof 檔案。
建立 ECS 任務定義
使用 Amazon ECS 主控台建立任務定義,作為您 Java 應用程式的藍圖。
對於環境變數,請提供您 S3 儲存貯體的名稱。應用程式的初始化命令也會使用該 S3 儲存貯體名稱。
**注意:**HEAP_DUMP_BUCKET 是範例命令中的環境變數。
建立 ECS 任務 IAM 角色
[建立一個包含 s3:PutObject 權限的 ECS 任務 AWS Identity and Access Management (IAM) 角色](https://docs.aws.amazon.com/AmazonECS/latest/userguide/task-iam-roles.html#create_task_iam_policy_and_role),讓您能上傳檔案。
執行初始化命令
在 JVM 工作目錄中,預設會建立一個名為 java_pidpid.hprof 設堆積傾印檔案。若要指定其他檔案名稱或目錄,請使用 XX:HeapDumpPath= 選項。
在下列範例初始化命令中,堆積傾印會在 /tmp/heap_dump.hprof 目錄中產生:
CMD ["java", \ "-XX:+HeapDumpOnOutOfMemoryError", \ "-XX:HeapDumpPath=/tmp/heap_dump.hprof", \ "-XX:OnOutOfMemoryError=yum install -y aws-cli > /dev/null && aws s3 mv /tmp/heap_dump.hprof s3://$HEAP_DUMP_BUCKET/'date+%F_%T'.hprof ; kill -9 %p", \ "MemoryApp" \]
初始化命令流範例
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請參閱AWS CLI 錯誤疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
OnOutOfMemoryError 選項指定在發生 OutofMemoryError 例外狀況時,要執行的命令或指令碼。有關 OnOutOfMemoryError 選項的更多資訊,請參閱 Oracle 網站上的 Java HotSpot VM 命令列選項。
完成以下 OnOutOfMemoryError 命令流程的步驟:
- 在無需互動的情況下,在 -y 內安裝 AWS Command Line Interface (AWS CLI)。命令輸出結果會儲存到 /dev/null 的暫存檔案,以避免將 AWS CLI 安裝過程中不必要的資訊加入容器日誌。
- 使用 ECS 任務角色的權限,將 .hprof 檔案上傳到 S3 儲存貯體。.hprof 檔案會重新命名為符合 date+%F_%T.hprof 模式的格式。
- 查看 Amazon CloudWatch 日誌資料:
2023-05-02T17:02:37.748+01:00 Hello, world! Hello Java! Good bye memory... 2023-05-02T17:02:37.748+01:00 Going to sleep for 2 ms... 2023-05-02T17:02:39.748+01:00 Wake now, lets heap the memory... 2023-05-02T17:02:39.748+01:00 java.lang.OutOfMemoryError: Requested array size exceeds VM limit 2023-05-02T17:02:39.748+01:00 Dumping heap to /tmp/heap_dump.hprof ... 2023-05-02T17:02:39.756+01:00 Heap dump file created [2893599 bytes in 0.008 secs] 2023-05-02T17:02:39.756+01:00 # 2023-05-02T17:02:39.757+01:00 # java.lang.OutOfMemoryError: Requested array size exceeds VM limit 2023-05-02T17:02:39.757+01:00 # -XX:OnOutOfMemoryError="yum install -y aws-cli > /dev/null && aws s3 mv /tmp/heap_dump.hprof s3://$HEAP_DUMP_BUCKET/`date +%F_%T`.hprof ; kill -9 %p" 2023-05-02T17:02:39.757+01:00 # Executing /bin/sh -c "yum install -y aws-cli > /dev/null && aws s3 mv /tmp/heap_dump.hprof s3://$HEAP_DUMP_BUCKET/`date +%F_%T`.hprof "... 2023-05-02T17:02:54.663+01:00 Completed 256.0 KiB/2.8 MiB (2.2 MiB/s) with 1 file(s) remaining Completed 512.0 KiB/2.8 MiB (4.4 MiB/s) with 1 file(s) remaining Completed 768.0 KiB/2.8 MiB (6.6 MiB/s) with 1 file(s) remaining Completed 1.0 MiB/2.8 MiB (8.7 MiB/s) with 1 file(s) remaining Completed 1.2 MiB/2.8 MiB (10.8 MiB/s) with 1 file(s) remaining Completed 1.5 MiB/2.8 MiB (12.9 MiB/s) with 1 file(s) remaining Completed 1.8 MiB/2.8 MiB (15.0 MiB/s) with 1 file(s) remaining Completed 2.0 MiB/2.8 MiB (17.0 MiB/s) with 1 file(s) remaining Completed 2.2 MiB/2.8 MiB (19.1 MiB/s) with 1 file(s) remaining Completed 2.5 MiB/2.8 MiB (21.1 MiB/s) with 1 file(s) remaining Completed 2.8 MiB/2.8 MiB (23.1 MiB/s) with 1 file(s) remaining Completed 2.8 MiB/2.8 MiB (13.0 MiB/s) with 1 file(s) remaining move: ../tmp/heap_dump.hprof to s3://fargate-test-cluster-logs/2023-05-02_16:02:53.hprof 2023-05-02T17:02:54.705+01:00 Executing /bin/sh -c "kill -9 1"... 2023-05-02T17:02:54.707+01:00 Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit 2023-05-02T17:02:54.707+01:00 at MemoryApp.main(MemoryApp.java:13)
上述日誌資料範例使用 yum install 來安裝 AWS CLI,且容器執行的是 Amazon Corretto 17 映像檔。如果容器映像檔是以不同的作業系統 (OS) 為基礎,則命令有所不同。以下是針對不同作業系統的範例命令:
- Fedora Linux:dnf install -y awscli
- Debian:apt-get install -y awscli
- Alpine Linux:apk add aws-cli
**注意:**無論使用哪種作業系統,您都必須使用與範例中,yum install 安裝 AWS CLI 命令等效的命令。
相關內容
已提問 2 年前
已提問 2 年前
已提問 3 年前
