Answer the question
In order to leave comments, you need to log in
Is it possible in Java 8 to set the memory limit based on system memory and not java process memory?
For example, if 95% is busy - do a dump and crash
Example - there is an instance with 4 GB of RAM and 2 java processes are running. And I want 2 processes to use all the memory, but if the system memory runs out, then kill the process and make a dump.
If I use -Xmx4g, then both consider that they can use 4GB and in total they can eat 8 GB and go into swap. if I write -Xmx2g, then both will fit, but if one process uses all 2 GB, then the second cannot use this memory.
Answer the question
In order to leave comments, you need to log in
In your case, there is no pure java solution. Here you need to charge a regular task (cron), which will look at the free physical memory in the system and kill the fattest java application when it reaches 95% of the memory limit. For example, like this:
#!/usr/bin/env bash
# pid'ы отслеживаемых java процессов
# задачку определения актуальных pid'ов оставлю на вас
PID1=1234
PID2=5678
# минимальное допустимое количество свободной памяти в процентах
MIN_MEM_SIZE=5
function get_java_mem {
jstat -gc $1 | awk 'NR==1 {print ($3 + $4 + $6 + $8 + $10)}'
}
function get_free_mem {
free | awk 'NR==2 {print ($7 * 100) / $2}'
}
if [ $(echo "`get_free_mem` < $MIN_MEM_SIZE" | bc) -eq "1" ]; then
if [ $(echo "`get_java_mem $PID1` > `get_java_mem $PID2`" | bc) -eq "1" ]; then
PID_TO_KILL=$PID1;
else
PID_TO_KILL=$PID2
fi
echo "Memory shortage detected. Taking heap dump and killing process $PID_TO_KILL"
jmap -dump:format=b,file=${PID_TO_KILL}.heapdump.bin $PID_TO_KILL
kill $PID_TO_KILL
fi
if I write -Xmx2g, then both will fit, but if one process uses all 2 GB, then the second cannot use this memory.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question