Get Max Heap Size of Running JVM from Command Line: No JDK Tools

Tags: java bash
By : Andrew
Source: Stackoverflow.com
Question!

I am working on a bash script that performs operations on our Java application web server prior to the web server going down for a restart/reboot. As part of this, I need to find out what was the maximum heap allocation that was provided to the JVM (the Xmx option). This value of this option will vary, depending on the hardware where the web application was deployed. I am looking for something I could use in a script to populate a variable like:

XMX_JVM_VALUE=$(command[s] here)

I will then be using this value to determine if there is enough space on our logging partition to create a heap dump there. That value will be gathered via:

LOG_DISK_SPACE=$(df -P /var/log | awk 'NR==2{print $4}')

The two restrictions I have is that I cannot use JDK tools as there is only the JRE on the server and I am limited to regular Linux commands that came with the system (Things like grep, ps, awk, etc...).

By : Andrew


Answers

After much trial and error, I came upon the following solution. It does use the assumption that the JVM process with the biggest Xmx value is the one of interest.

DATA_UNIT_MULTIPLIER=1024
XMX_JVM_VALUE=$(ps -ef | grep -oP "Xmx\d+(m|g)" | sort -rn | cut -d'x' -f2 | head -1)
XMX_DATA_UNIT="${XMX_JVM_VALUE:$((${#XMX_JVM_VALUE}-1)):1}"
XMX_JVM_NUM=$(echo $XMX_JVM_VALUE | sed '$s/.$//')

XMX_SIZE=1
if [ "$XMX_DATA_UNIT" = "m" ]; then
    XMX_SIZE=$(($XMX_JVM_NUM * $DATA_UNIT_MULTIPLIER ))
elif [ "$XMX_DATA_UNIT" = "g" ]; then
    XMX_SIZE=$(($XMX_JVM_NUM * $DATA_UNIT_MULTIPLIER * $DATA_UNIT_MULTIPLIER))
fi

This implementation converts the value of Xmx into the same units being reported by df. It also take into consideration the unit suffix on the Xmx option.

By : Andrew


How about...

java -XX:+PrintFlagsFinal 2> /dev/null | grep MaxHeapSize

The java command is available in the JRE.



If you don't mind using a 3rd-party package, you could do it with pandas.

import pandas as pd
pd.DataFrame(test_dict).to_csv('test.csv', index=False)

update

So, you have several dictionaries and all of them seems to come from a scraping routine.

import pandas as pd

test_dict = {"review_id": [1, 2, 3, 4],
             "text": [5, 6, 7, 8]}
pd.DataFrame(test_dict).to_csv('test.csv', index=False)

list_of_dicts = [test_dict, test_dict]
for d in list_of_dicts:
    pd.DataFrame(d).to_csv('test.csv', index=False, mode='a', header=False)

This time, you would be appending to the file and without the header.

The output is:

review_id,text
1,5
2,6
3,7
4,8
1,5
2,6
3,7
4,8
1,5
2,6
3,7
4,8
By : gabra


This video can help you solving your question :)
By: admin