Skip to content

JVM运维相关知识

This page demonstrates some of the built-in markdown extensions provided by VitePress.

常用运维参数

打印类加载信息,项目启动时增加参数(生产环境不推荐打开):

md
-verbose:gc -XX:+PrintGCDetails

打开统计信息(生产环境不推荐打开):

md
-XX:NativeMemoryTracking=summary
例子:
opt = " -Dspring.profiles.active=pro"
opt += " -Dserver.port=8081 -DworkId=4"
opt += " -Xms3072m -Xmx3072m -Xmn2048m"
cmd = "nohup " + START + " -XX:NativeMemoryTracking=summary -jar" + opt + " " + API_NAME + " >/dev/null 2>&1 &"

常用运维命令

查看进程的线程数和资源占用情况:
  top -H -p 19222 查看线程运行代码位置,把线程id转16进制:
  pid: 20576 ->(16进制)5060;linux十进制转十六进制:printf '%x\n' 15
  linux十六进制转十进制:printf '%d\n' 0xF
查看19667,线程id2057对应的16进制(注意字母要小写)5060最后50行执行的代码位置:
  jstack 19054 | grep -A 50 4a72
jmap:
  jmap -dump:format=b,file=19054.dump 19054,导出内存dump
  jmap -heap pid(查看进程各分代大小及使用情况)
jstat:
  jstat -gcutil pid(查看进程各分代使用情况)
  jstat -gcutil pid 1000 100(每秒打印一次进程各分代使用情况,共打印100次)
  jstat -gc pid (查看各分代大小和gc情况)
java:
  linux解压jar包:jar -xvf hello.jar
堆外内存溢出排查:
  1.开启class加载日志:启动参数增加 -verbose:class或者-XX:+TraceClassLoading
  2.查看重复加载的class,然后分析重复加载原因
jcmd:
  jcmd pid help(查看当前pid可用命令)

查看内存使用情况

md
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import re
import time
import sys

#用gcutil查看项目
def gcutil(pid):
    cmd = 'jstat -gcutil ' + pid + ' 1000 100'
    os.system(cmd)

#使用gc查看项目内存使用情况
def gc(pid):
    cmd = 'jstat -gc ' + pid
    res = os.popen(cmd)
    i = 0
    names = []
    for l in res:
        i = i + 1
        if i == 1:
            nameStr = re.sub(" +"," ",l.strip())
            names = nameStr.split(" ")
        if i != 1:
            usedStr = re.sub(" +"," ",l.strip())
            used = usedStr.split(" ")
            j = 0
            for u in used:
                print(names[j] + " : " + u)
                j = j + 1
            sum = float(used[0]) + float(used[1]) + float(used[4]) + float(used[6]) + float(used[8]) + float(used[10])
            print('used: ' + str((sum / 1024)))


#jmap -heap
def jmap(pid):
    cmd = 'jmap -heap ' + pid
    os.system(cmd)

if __name__ == '__main__':
    args = sys.argv
    size = len(args)
    if size < 2:
        print('place input gc/gcutil/jmap pid....................')
    else:
        opt = args[1]
        if opt == 'gc':
            gc(args[2])
        if opt == 'gcutil':
            gcutil(args[2])
        if opt == 'jmap':
            jmap(args[2])

打印程序运行栈

md
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import re
import time
import sys

#打印进程栈运行情况
def print_jstack(pid):
    cmd = 'jstack ' + str(pid)
    print(cmd)
    jstack_file = str(pid) + '.jstack'
    print(jstack_file)
    res = os.popen(cmd)
    with open(jstack_file,'w') as f:
        f.write('')
    for line in res:
        print(line)
        with open(jstack_file,'a') as a:
            a.write(line)

if __name__ == '__main__':
    args = sys.argv
    if len(args) < 2:
        print('请输入进程号')
    if len(args) >= 2:
        print_jstack(args[1])

```

记录CPU使用超过100%进程

md
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
import time
import sys
import os

FILE_PATH = '/usr/local/projects/monitor/log/'

#获取当前时间:yyyyMMDD
def getDate():
    now = int(time.time())
    timeStruct = time.localtime(now)
    timeStr = time.strftime("%Y%m%d", timeStruct)
    return timeStr

#获取当前时间:yyyyMMDDHHmmss
def getTime():
    now = int(time.time())
    timeStruct = time.localtime(now)
    timeStr = time.strftime("%Y%m%d%H%M%S", timeStruct)
    return timeStr


#打印top命令
def printTop():
    cmd = 'top -b -n 1'
    lines = os.popen(cmd)
    i = 0
    for v in lines:
        i = i +1
        if i >= 8:
            res = re.sub('\x1b.*?m', '', v)
            res = re.sub(' +',' ',res)
            split = res.strip().split(" ")
            if len(split) > 10:
                check_cpu(split[0],split[8],split[9])

#def检查进程cpu占用情况
def check_cpu(pid,cpu,memory):
    cpu = float(cpu)
    if cpu > 100:
        cmd = 'ps -ef | grep -v grep | grep ' + pid
        lines = os.popen(cmd)
        for l in lines:
            append_file(l)

#def写入文件
def append_file(line):
    file_name = FILE_PATH + getDate() + '.log'
    with open(file_name,'a') as f:
        line = getTime() + ' : ' + line
        f.write(line)

if __name__ == '__main__':
   printTop()

```

More

Check out the documentation for the full list of markdown extensions.