Application Server Memory

There are times when a nice quick and simple way of diagnosing memory usage is required. Sure there are lots of amazing and powerful tools, however what follows is a very simple solution!

<%@ page import="java.lang.management.*, java.util.*" %>
<%response.setContentType("text/plain");
  Iterator iter = ManagementFactory.getMemoryPoolMXBeans().iterator();
  while(iter.hasNext()) {
    MemoryPoolMXBean item = (MemoryPoolMXBean) iter.next();
      MemoryUsage mu = item.getUsage();
      long used      = mu.getUsed();
      long committed = mu.getCommitted();
      long max       = mu.getMax();
      long usedMB       = (long)(used/(1024*1024));
      long committedMB  = (long)(committed/(1024*1024));
      long maxMB        = (long)(max/(1024*1024));
%>
MEMORY TYPE: <%=item.getName()%>
Used:        <%=used%>  (<%=usedMB%>MB)
Committed:   <%=committed%>  (<%=committedMB%>MB)
Max:         <%=max%>  (<%=maxMB%>MB)
<%}%>

Whilst this won't fix issues like java.lang.OutOfMemoryError: PermGen space it will help show whether other areas of memory are also having an issue and possibly allow you to get early sight of the problem.