AWS re:Post을(를) 사용하면 다음에 동의하게 됩니다. AWS re:Post 이용 약관

Java heap dump file 파일을 S3 버킷으로 내보내려면 어떻게 해야 하나요?

4분 분량
0

Amazon Elastic Container Service(Amazon ECS)에 저장된 Java 애플리케이션에서 “heap out of memory” 오류가 발생했습니다. 이 오류를 해결하기 위해 .hprof 파일을 생성하여 Amazon Simple Storage Service(S3) 버킷에 저장하려고 합니다.

간략한 설명

OutOfMemoryError 예외가 발생하는 가장 일반적인 이유는 Java 가상 머신(JVM)이 힙에 충분한 메모리를 할당할 수 없기 때문입니다. Java 애플리케이션이 ECS 컨테이너에서 실행되는 경우 "힙 메모리 부족" 오류가 발생하면 애플리케이션이 중단되고 ECS 작업이 중지됩니다. 애플리케이션이 저장하는 모든 .hprof 파일을 더 이상 사용할 수 없습니다. 예외 오류 문제를 해결하고 메모리 할당 문제를 디버그하려면 .hprof 파일을 생성하여 S3 버킷에 저장해야 합니다.

해결 방법

참고: 다음 해결 방법은 대부분의 OutOfMemoryError 예외 시나리오를 해결합니다. Java 메모리 오류의 원인에 대한 자세한 내용은 Oracle 웹사이트에서 OutOfMemoryError 예외 이해하기를 참고하세요.

S3 버킷 생성

Amazon S3 콘솔을 사용하여 .hprof 파일을 저장할 S3 버킷을 생성합니다.

ECS 작업 정의 생성

Amazon ECS 콘솔을 사용하여 Java 애플리케이션의 청사진으로 작업 정의를 생성합니다.

환경 변수에 S3 버킷의 이름을 입력합니다. 애플리케이션의 초기화 명령도 S3 버킷 이름을 사용합니다.

참고: 예제 명령의 환경 변수는 HEAP_DUMP_BUCKET입니다.

ECS 작업 IAM 역할 생성

파일을 업로드할 수 있도록 s3:PutObject 권한을 포함하는 ECS 작업 AWS Identity and Access Management(IAM) 역할을 생성합니다.

초기화 명령 실행

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 명령 흐름에 대한 다음 단계를 완료합니다.

  1. 상호 작용 없이 -y 내에 AWS Command Line Interface(AWS CLI)를 설치합니다. 명령 출력은 /dev/null의 임시 파일에 저장되므로 AWS CLI 설치에 대한 불필요한 정보가 컨테이너 로그에 추가되지 않습니다.
  2. ECS 작업 역할의 권한으로 .hprof 파일을 S3 버킷에 업로드합니다. .hprof 파일의 이름은 date+%F_%T.hprof 패턴을 따르도록 변경됩니다.
  3. 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)

앞의 로그 데이터 예제에서는 Amazon Corretto 17 이미지를 실행하는 컨테이너의 AWS CLI에 yum install를 사용합니다. 컨테이너 이미지가 다른 운영 체제(OS)를 기반으로 하는 경우 명령이 변경됩니다. 다음은 다른 운영 체제에 대한 명령 예제입니다.

  • Fedora Linux: dnf install -y awscli
  • Debian: apt-get install -y awscli
  • Alpine Linux: apk add aws-cli

**참고:**모든 OS에서 AWS CLI를 설치하는 yum install 명령의 예와 동일한 명령을 사용해야 합니다.