堆外内存
bash -s -- java进程号 <<'JVM_MEM'
set -o pipefail
export LC_ALL=C
pid=$1
java=$(readlink "/proc/$pid/exe") || { echo "读取 PID $pid 失败,请核对 PID 和运行账号。" >&2; exit 1; }
jcmd=${java%/*}/jcmd
if [[ ! -x $jcmd ]]; then
jcmd=$(command -v jcmd) || { echo "找不到 jcmd,请把目标 JDK 的 bin 目录加入 PATH。" >&2; exit 1; }
fi
"$jcmd" "$pid" System.native_heap_info | awk '
BEGIN { RS = ">"; gib = 1024^3 }
function attr(tag, name, field) {
if (!match(tag, "[[:space:]]" name "="[^"]"")) return ""
field = substr(tag, RSTART, RLENGTH)
sub(/["]"/, "", field)
sub(/"$/, "", field)
return field
}
{
tag = $0
sub(/[<]*</, "", tag)
if (tag ~ /^malloc[[:space:]]/) active = 1
if (!active) next
if (tag ~ /^heap[[:space:]]/) { arenas++; in_heap = 1 }
if (tag == "/heap") in_heap = 0
if (tag == "/malloc") { complete = 1; active = 0 }
glibc 已提供全局汇总,跳过各分配区明细,避免重复计算。
if (in_heap || tag !~ /^(system|total)[[:space:]]/) next
kind = attr(tag, "type")
bytes = attr(tag, "size")
if (bytes !~ /[1]+$/) next
if (tag ~ /^system[[:space:]]/ && kind == "current") size["arena"] = bytes + 0
if (tag ~ /^total[[:space:]]/) size[kind] = bytes + 0
}
END {
if (!complete || !arenas || !("arena" in size) || !("fast" in size) || !("rest" in size)) {
print "未取得完整的 glibc 统计,请检查目标 JVM 是否支持 System.native_heap_info。" > "/dev/stderr"
exit 1
}
printf "glibc 分配区数量 %d\n", arenas
printf "分配区总持有 %.3f GiB\n", size["arena"] / gib
printf "其中账面空闲 %.3f GiB\n", (size["fast"] + size["rest"]) / gib
if ("mmap" in size) printf "另计的独立 mmap %.3f GiB\n", size["mmap"] / gib
}'
JVM_MEM
0-9 ↩︎

浙公网安备 33010602011771号