Monday, October 25, 2010

The horrible Linux out-of-memory killer and Java


The symptoms : your favorite Java process is being terminated with no apparent cause. sometimes you may see the mysterious "Killed" as a last note on the stderr, but sometimes even this last grace will be denied.

To verify : cat /var/log/syslog | grep "Out of memory" , and look for your process id. 

What is the out-of-memory killer (hence OOMK), and why it is horrible : some facts (and views) can be found in the following links :
I'll just summarize by saying it's like a mad banker - it gives the client (a process) IOU notes ( "memory" ), when the client wishes to cash-in (use the memory) , it gives it some real money (physical memory - ram or swap) , but if there is currently not enough money, it selects one of the clients and kills him ...


What to do : remember , the fact process X had been chosen by the OOMK does not mean it is leaking, or even consuming more memory than before.  in fact, in the specific case I was exploring, it seems the OOMK was very fond of killing Java (and mysql server ) processes , probably due to their memory allocation pattern - i.e. allocating ,and using, large self-managed buffers . 

So, in my case, the solution was to monitor the system carefully with the following script (let's call it psmon)
#!/bin/bash

FREQ=60

while getopts pmf:t OPT; do
case "$OPT" in
    p)    PROCESSMEM="true"
        ;;
    m)    SYSMEM="true"
        ;;
    f)    FREQ=$OPTARG
         ;;
     t)    TIMESTAMP="true"
        ;;
    esac
done

while [ 1 ]; do
    echo "-------------------------------------------"
    if [ "$TIMESTAMP" == "true" ]; then
        date +%s
    fi
    if [ "$PROCESSMEM" == "true" ]; then
        ps -eo pid,ppid,rss,vsize,pcpu,pmem,cmd -ww --sort=pid
    fi
    if [ "$SYSMEM" == "true" ]; then
        free -mt
    fi
    sleep $FREQ   
done

for example :
psmon -f 1 -p -m -t >/tmp/psmon.out &
and then run the application, and wait for the OOMK.

I could then read from the syslog the time when the  OOMK was invoked ,
and look at the memory consumption per process just prior to that time, and sure enough, I've found a very short , very high peak usage of both RAM and virtual memory in one of our native processes (which was never harmed by the OOMK).








 



Wednesday, October 13, 2010

a simple tool to detect java over allocation

A while ago, I was exploring a memory leak (well, I thought it was a leak - but more on that later) , when I came across the problem of running memory analysis on a large, live application .
Sure, there are plenty of tools - MAT, and jvisualvm , to mention the best I've found - but it turned out these were too heavy-weight for me (the cost of dumping / transferring the heaps and/or running the GUI itself) .
what I've found useful, was to use simple multiple histograms obtained from jmap, and to use MySQL as the data analyzer. 
so , here are the very modest scripts (bash) :

import the histogram as a mysql table :
hisport
#!/bin/bash
MYSQL="mysql $DB"
FILE=$1
NAME=$2
if [ "$FILE" == "" ] || [ "$NAME" == "" ];then
    echo "usage : $0 <file> <name> - import jvm histogram <file> into table <name> using $DB"
fi
CLEAN=`mktemp`
cat $FILE | sed 's/\s\+/,/g' | cut -c2- > $CLEAN
$MYSQL  -e"create table $NAME (num int,inst int,mem int,name varchar(256));"
$MYSQL  -e"LOAD DATA LOCAL INFILE \"$CLEAN\" INTO TABLE $NAME FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ignore 2 lines  (num,inst,mem,name);"


compare dead+live to live only objects  : this can hint on the type of objects which are being allocated and dropped over and over , i.e. garbage collected frequently (you should be able to get similar info from the gc itself , using -xlogcc or other JVM interfaces - what's nice about these scripts is that they can be ran on a live JVM without any pre-launch changes)
hispare
#!/bin/bash
MYSQL="mysql $DB"
dead=$1
live=$2
echo "select $dead.* ,$live.* ,$dead.inst - $live.inst diff from $live inner join $dead  on $dead.name=$live.name order by  diff DESC " | $MYSQL

putting it together:
# dump a live + dead histogram, import both and compare them :
$JAVA_HOME/bin/jmap -histo:live <pid> >pid.live.histo
$JAVA_HOME/bin/jmap -histo <pid> >pid.dead.histo
export DB=<DBNAME>
mysql -e"create database $DB"
./hisport pid.live.histo LIVE
./hisport pid.dead.histo DEAD
./hispare DEAD LIVE