First, find PID of the Java process (e.g by using jps -lvm
).
Use jstat
to get measurements on the usage/capacity of the several memory pools of the heap (see also man jstat
). The results are reported in kbytes.
Get heap capacity, usually referred to as commited size (i.e memory allocated from the OS), by summing up S0C
, S1C
, EC
, OC
columns:
jstat -gc ${pid} | gawk '{if (NR > 1) {printf("%.0fk\n", ($1 + $2 + $5 + $7))}}'
Get heap usage (i.e memory actually used by objects), by summing up S0U
, S1U
, EU
, OU
columns:
jstat -gc ${pid} | gawk '{if (NR > 1) {printf("%.0fk\n", ($3 + $4 + $6 + $8))}}'
If we need to also account for metaspace capacity/usage (i.e class metadata), then we must also add columns MC
/MU
($9
/$10
) respectively.