[java]dump 관련

heap dump

  • A heap dump is a snapshot of all the objects that are in memory in the JVM at a certain moment.
    • hang 걸릴 가능성 있음.
  • 생성방법

      ```
      jmap -dump:[live],format=b,file=<file-path> <pid>
      ```
      ```
      live: if set it only prints objects which have active references and discards the ones that are ready to be garbage collected. This parameter is optional
      format=b: specifies that the dump file will be in binary format. If not set the result is the same
      file: the file where the dump will be written to
      pid: id of the Java process
      ```
      * example
          ```
          jmap -dump:live,format=b,file=/tmp/dump.hprof 12587
          ```   * `jcmd <pid> GC.heap_dump <file-path>`
      * example
          ```
          jcmd 12587 GC.heap_dump /tmp/dump.hprof
          ```   * JVisualVM
      * ![image](https://brush-up.github.io/assets/images/2020-06-26-java-dump-001.png)   * 자동으로 heap dump 생성하기
      *  java.lang.OutOfMemoryError 발생시 자동으로 생성하려면 아래와 같이 옵션주기.
      ```
      java -XX:+HeapDumpOnOutOfMemoryError
      java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<file-or-dir-path>
      ```
    

thread dump

  • A thread dump is a snapshot of the state of all the threads of a Java process.
  • 생성방법
    • jstack [-F] [-l] [-m] <pid>
        -F option forces a thread dump; handy to use when jstack pid does not respond (the process is hung)
        -l option instructs the utility to look for ownable synchronizers in the heap and locks
        -m option prints native stack frames (C & C++) in addition to the Java stack frames
      
      • example
          jstack 17264 > /tmp/threaddump.txt
        
    • jvisualvm 툴 이용하기
      • https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jvisualvm.html
      • image
  • $ kill -3 <pid>
  • $ jstack <pid> > <filename>
    • 부하가 kill -3보다 더 있음 cpu 100인 상태에선 kill -3을 이용하자.
  • 분석은
    • JVisualVM
    • fastThread 사이트
      • https://fastthread.io/

음..

이걸 보면 ms가 확실히 dump관련해선 잘 만들어놨구나란 생각이.. 예전에 작성하다 더이상 추가작성하지 않던 문서..

  • 참고
    • https://www.baeldung.com/java-thread-dump
    • https://www.baeldung.com/java-heap-dump-capture