Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active May 10, 2020 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmalex07/1198dbce5ad95defc387890001d3a353 to your computer and use it in GitHub Desktop.
Save drmalex07/1198dbce5ad95defc387890001d3a353 to your computer and use it in GitHub Desktop.
Get Java heap usage. #java #heap #memory

README - Get Java heap usage

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment