How to monitor java application memory usage in Docker
To connect to a java process running in a docker container running in boot2docker
with visualvm
you can try the following:
Start your java process using the following options:
java -Dcom.sun.management.jmxremote.port=<port> \-Dcom.sun.management.jmxremote.authenticate=false \-Dcom.sun.management.jmxremote.ssl=false \-Dcom.sun.management.jmxremote.rmi.port=<port> \-Djava.rmi.server.hostname=<boot2docker_ip> \<Main>
You need to run your image with --expose <port> -p <port>:<port>
.
Then "Add JMX Connection" in visualvm
with <boot2docker_ip>:<port>
.
It shouldn't be much different without boot2docker
.
To monitor it's usage, you need to get it's real Process ID. If you are running tomcat directly in the container, then it should be:
DOCKER_ROOT_PROC=`(docker inspect -f "{{ .State.Pid }}" my_container)`
If you are using something like Phusion's baseimage, then your java process will be a child of that process. To see the hierarchy use:
pstree $DOCKER_ROOT_PROC
Once you have that, you can write your script using
ps -o pid,cmd --no-headers --ppid $DOCKER_ROOT_PROC
In your script recursively to find the java process you want to monitor (with some Regular Expression filtering, of course). Then finally you can use this to get your java application's memory usage in kilobytes:
ps -o vsz -p $JAVAPROCESS
I don't know if this can be used with jconsole, but it is a way of monitoring the memory usage.
To monitor docker containers I recommend Google's cAdvisor
project. That way you have a general solution to monitor docker containers. Just run your app, whatever that is, in a docker container, and check things like cpu and memory usage. Here you have an http API as well as a web ui.