JVM

概念

内存空间

  • 程序计数器
  • 虚拟机栈
  • 本地方法栈
  • 方法区
  • 运行时常量池
  • 直接内存

垃圾回收

  • 算法

    • 引用计数法
    • 可达性分析法
  • 分配回收策略

    • Young
    • Old
  • 回收器

    • When to choose SerialGC, ParallelGC over CMS, G1 in Java?

    • Serial:Mainly for single-cpu machine.

    • Parallel:It uses multiple gc threads to handle heap, and perform stop-the-world pause during any gc.

    • CMS:It’s designed to eliminate the long pause associated with the full gc of parallel & serial collector.

    • G1:It’s low pause / server style gc, mainly for large heap (> 4Gb).

      • Since Java 9, this is default for server-class machine

ES 配置示例

## GC configuration
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly

## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30

GC 日志:https://elasticsearch.cn/article/812

JDK 工具

jps

类似于 ps 命令,显示 vmid

jps

jps -l # 显示主类全名

jps -v # 显示允许参数

jinfo

显示 vm 参数

jinfo <pid>

jstat

统计监控工具

# 统计类装载、卸载、空间、时间
jstat -class <pid>

# 监控 Java heap 状况
jstat -gc <pid>
jstat -gcutil <pid>

jmap

生成 heap 转储快照

jmap <pid>

# 显示堆详细信息
jmap -heap <pid>

# 生成转储快照
jmap -dump:format=b,file=es.bin <pid>

jstack

生成虚拟机当前时刻的线程快照

# 除堆栈外,显示关于锁的附加信息
jstack -l <pid>