Sunday, May 19, 2013

Monitoring Memory Usage inside a Java Web Application with JSF, PrimeFaces and MemoryMXBean

This article explains how to monitor memory usage in your web application by requesting the MemoryMXBean and exposing collected metrics within a Primefaces LineChart component in a JSF page.


The monitoring of the memory usage involves:

The overall design is illustrated below:

  1. The LineChart component calls every minute the MonitorController instance.
  2. The MonitorController instance requests the MemoryMXBean instance to get the current memory usage.
  3. The MonitorController instance updates a CartesianChartModel instance by adding a memory usage snapshot.
  4. The CartesianChartModel instance is returned to the LineChart component that renders it inside a JSF page.

PrimeFaces LineChart

To enable PrimeFaces components into your web application, update your maven pom.xml with the following repository and dependency:
            PrimeFaces Maven Repository
Next, inside a JSF page, add the following code to display the LineChart and make it polling the MonitorController every minute:
<html xmlns=""


                <h:form id="form">  

                    <!-- polls every minute -->
                    <p:poll interval="60" update="memoryChart" />  

                    <!-- memory line chart -->
                    <p:lineChart id="memoryChart" value="#{monitorController.memoryModel}"
                                 legendPosition="ne" title="Memory Usage" style="height:300px;margin-top:20px"
                                 xaxisLabel="Minutes" yaxisLabel="Bytes" zoom="true"/>  




The code corresponding to the monitor controller class is:
package org.javabenchmark;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.slf4j.LoggerFactory;

 * JSF Managed Bean to get JVM memory usage.
public class MonitorController implements Serializable {

     * memory model.
    private CartesianChartModel memoryModel;
     * memory usage series.
    private ChartSeries memoryUsageSerie, maxMemorySerie;
     * elapsed time in minutes since the monitoring starts
    private long elapsedTime = -1;

     * instantiates a new monitoring controller.
    public MonitorController() {
     * initializes memory usage model.
    private void createMemoryModel() {  
        // model
        memoryModel = new CartesianChartModel(); 
        // heap serie
        memoryUsageSerie = new ChartSeries();  
        // max serie
        maxMemorySerie = new ChartSeries();  
     * gets the memory model that will be rendered within a LineChart
     * component. The LineChart component should call this method every minute.
     * @return the updated memory model
    public CartesianChartModel getMemoryModel() {  
        // gets data
        MemoryMXBean memoryMxBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage memoryUsage = memoryMxBean.getHeapMemoryUsage();
        // one more minute
        // updates series
        memoryUsageSerie.set(elapsedTime, memoryUsage.getUsed());
        maxMemorySerie.set(elapsedTime, memoryUsage.getMax()); 
        return memoryModel;  


Finally, a screenshot of the PrimeFaces line chart representing the memory usage :

Notice that it is possible to zoom with drag and drop, if you have a lot of points displayed.


As you can see, it is quite easy to monitor the JVM and to display collected metrics within a JSF page. You can go further by monitoring CPU usage for instance, or by adding some features like starting/stopping/resetting the monitoring.


  1. Awesome! Thanks!!!!!!!!

  2. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

  3. Thank you for sharing this. It was a great guide on how to blog properly. Articles you have it is very clear and detailed here. Look forward to your writing in the future.. Load Test Website

  4. Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.morephp jobs in hyderabad.

  5. I have read your blog its very attractive and impressive. I like it your blog. upload to instagram from mac

  6. This topic is something that I have been looking into for a while now and your insight is exceptional. Thanks for sharing this information instagram web viewer

  7. Your blog is so nice that every one would feel pleasure to post comments. So am I. Thanks. instagram web viewer

  8. Thank you for sharing this article. It is really useful for me.

  9. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power Bi Online course

  10. Thanks a lot very much for the high quality and results-oriented help.
    I won’t think twice to endorse your blog post to anybody who wants
    and needs support about this area.

    java Training in Bangalore

    java training in chennai

  11. This is an awesome post.Really very informative and creative contents.This concept is a good way to enhance the knowledge.
    Java training in Chennai

  12. Thanks for sharing this informative article..!!
    Keep posting waiting for next post

    Java Training in Bangalore

  13. simply I experience your web page furthermore practising and beneficial opinion. it is protected enormously handy calculation as soon as a variety of our assets.thanks for element. i enjoy this make acknowledged. Application Modernization

  14. piercing might not provide a great electrical connection because of the fistula that forms around the anchor. I've considered bench tests with hypodermic needles to assess the efficacy of transdermal electrodes, but that still won't shed any light on what effect the healing process might have.


  15. Its a good post and keep posting good article.its very interesting to read.
    Data Science Course in Chennai