example of how to set java max heap size dynamically

Post author
Andrew Davidson

If your command runs a java program, increase the runtime memory will probably fix 'out of memory' error. The example shows how to dynamically calculate the max heap size on linux based docker images

enjoy

share

```

#
# find amount of free memory and unused swap
#
free --total --bytes | grep Total | tee free.out
echo
# Total: 1653340639232 435497533440 59898118144

# looks like tab is replace with 4 space. Is the a mac terminal thing?
# replace all white space to make sparcing portabl
sed -e 's/\s\+/,/g' free.out | tee sed.out
echo
# Total:,1653340639232,435497533440,59898118144

# parse out last numeric value
freeBytes=`cut -d , -f 4 sed.out`
printf "freeBytes: $freeBytes \n"
#59898118144

# make sure we have multiple of 1024 bytes

quotient=$(expr $freeBytes / 1024)
printf "quotient: $quotient \n"
# 58494256


# TODO: make sure we have more than 2 mb

MAX_HEAP=`echo "$quotient"K`
printf "MAX_HEAP: $MAX_HEAP \n"

java \
-Xmx"$MAX_HEAP" \
-XshowSettings:vm \
-jar /usr/gitc/picard.jar SamToFastq \

```

 

Comments

0 comments

Please sign in to leave a comment.