Search code examples
bashshellawksedcut

Calculating TotalHeap Size in G1GC


How do i Read G1 Heap used variable using a shell script in the below example. i tried using awk with no luck

jmap -heap

Heap Usage:
G1 Heap:
regions  = 2048
capacity = 8589934592 (8192.0MB)
used     = 4976717264 (4746.167434692383MB)
free     = 3613217328 (3445.832565307617MB)
57.93661419302225% used

G1 Young Generation:
Eden Space:
regions  = 234
capacity = 3854565376 (3676.0MB)
used     = 981467136 (936.0MB)
free     = 2873098240 (2740.0MB)
25.462459194776933% used
Survivor Space:
regions  = 14
capacity = 58720256 (56.0MB)
 used     = 58720256 (56.0MB)
free     = 0 (0.0MB)
100.0% used

G1 Old Generation:
regions  = 941
capacity = 4676648960 (4460.0MB)
used     = 3936529872 (3754.167434692383MB)
free     = 740119088 (705.8325653076172MB)
84.17415772852877% used

Solution

  • Could you please try following. I am using exit so when first match found for Heap Usage and then first match of string used is found it will print requested value and exit immediately from program, so this will be faster also.

    awk '/Heap Usage:/{found=1;next} found && /used/{print $3;exit}'  Input_file