分类 Java 相关 下的文章

Java 常见的三种 dump 文件: Core Dump, heap dump, thread dump

  1. core dump: 有时又被称作 crash dump, 它是某个进程在某个时间点的内存镜像. 它既可以在系统发生致命错误或无法处理的错误时候自动产生, 也可以通过命令工具产生. 但是 core dump 一遍并不能包含所有的内存页,但至少包含 heap 和 stack 的内存信息. core dump 默认放在当前进程的工作文件夹,并以core.pid 命名. 使用 jmap 转换 core dump 为 HPROF 文件或使用 Java VisualVM 来分析都可以.
  2. heap dump: 内存某个时间点镜像, 有ASCII 或 binary 格式,不同格式可能包含的信息不一样. 但一般包含 heap 里的类和对象实例信息. 但不包含 when & where 对象产生.
  3. thread dump: The thread dump consists of the thread stack, including thread state, for all Java threads in the virtual machine. The thread dump does not terminate the application: it continues after the thread information is printed.

如何使用 Java VisualVM 来分析 core dump
转换 core dump 为 HPROF 文件

_$jmap -dump:format=b,file=dump.hprof /usr/bin/java core.1234  //这个 java 是你 core dump 运行的 java
  • 如何产生 core dump
  • 如何产生 heap dump or 这里 or 这里
    -- 应用启动时: _$ java -agentlib:hprof=file=snapshot.hprof,format=b application
    -- 应用启动时: _$ java -XX:+HeapDumpOnOutOfMemoryError application
    -- _$ jcmd <process id/main class> GC.heap_dump filename=Myheapdump
    -- _$ jmap -dump:format=b,file=snapshot.jmap pid
    -- 使用 JConsole 工具
  • 如何产生 thread dump
    -- _$ jstack -f 5824
    -- use VisualVM

关于 Java Cert, keystore

keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts Java default password: changeit

openssl s_client -connect google.com:443

https://en.wikipedia.org/wiki/Keystore
http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/keytool.html

JVM - jdb

jdb 是 Hotspot JDK 里面的一个命令行debug 实用工具。
实用 Java Debug Interface (JDI) 连接目标JVM。

JVM HPROF 笔记

HPROF 是一个 DDL 格式的工具, 用来做 JVM heap 和 CPU profiling 的,在每个 JDK 里面都有. 它把 profiling 信息要么写到文件, 要么以二进制或 ASCII 码格式写到 socket. 这些 profiling 工具可以被其它工具使用.

HPROF is a tool for heap and CPU profiling shipped with every JDK release. It is a dynamic-link library (DLL) that interfaces with the Java Virtual Machine (JVM) using the Java Virtual Machine Tool Interface (JVM TI). The tool writes profiling information either to a file or to a socket in ASCII or binary format. This information can be further processed by a profiler front end tool.

The HPROF tool is capable of presenting CPU usage, heap allocation statistics, and monitor contention profiles. In addition, it can report complete heap dumps and states of all the monitors and threads in the JVM. In terms of diagnosing problems, HPROF is useful when analyzing performance, lock contention, memory leaks, and other issues.

源代码在: $JAVA_HOME/demo/jvmti/hprof 目录
常用命令:

  1. java -agentlib:hprof=help
  2. java -agentlib:hprof ToBeProfiledClass
  3. java -agentlib:hprof=heap=sites ToBeProfiledClass
  4. java -agentlib:hprof=cpu=samples,interval=20,depth=3 classname
  5. java -agentlib:hprof=heap=sites classname

Option Name and Value Description Default


heap=dump|sites|all heap profiling all
cpu=samples|times|old CPU usage off
monitor=y|n monitor contention n
format=a|b text(txt) or binary output a
file= write data to file java.hprof[{.txt}]
net=: send data over a socket off
depth= stack trace depth 4
interval= sample interval in ms 10
cutoff= output cutoff point 0.0001
lineno=y|n line number in traces? y
thread=y|n thread in traces? n
doe=y|n dump on exit? y
msa=y|n Solaris micro state accounting n
force=y|n force output to y
verbose=y|n print messages about dumps y

JVM Troubleshooting 学习笔记

Java Platform, Standard Edition Troubleshooting Guide

Command-line options that are prefixed with -XX are specific to Java HotSpot VM. For more information about command-line options used by Java HotSpot VM, see Java HotSpot VM Command-Line Options.
The jcmd is a new JDK profiling utility in JDK 8. It is suggested to use the latest diagnostic utility, jcmd instead of the earlier jstack, jinfo, and jmap utilities.

Java SE Monitoring and Management Guide
Monitoring and Management for the Java Platform