Pages

Monday 28 November 2011

JSP/Servlet-Display Server Memory Details

Memory.jsp

<%@ page import="java.lang.Runtime" %>
<%@ page import="java.util.Date" %>
<%
Runtime rt = Runtime.getRuntime();
rt.gc();
Float Free;
Float Total;
Float Used;
long iFree;
long iTotal;
 
String strTimeStamp; /* The current date/time as a string: YYYY-MM-DD HH:MM */
String strTsYear; /* The current year as a string : YYYY */
String strTsHour; /* The current hour as a string : HH */
String strTsMon; /* The current monthh as a string : MM */
String strTsDay; /* The current day as a string : DD */
String strTsMin; /* The current minute as a string : MI */
String strTsSec; /* The current second as a string : SS */
Date ts;
ts = new java.util.Date();
strTsYear = (new Integer(ts.getYear() + 1900)).toString();
strTsMon = (new Integer(ts.getMonth() + 1)).toString();
strTsDay = (new Integer(ts.getDate())).toString();
strTsHour = (new Integer(ts.getHours())).toString();
strTsMin = (new Integer(ts.getMinutes())).toString();
strTsSec = (new Integer(ts.getSeconds())).toString();
if (strTsMin.length() == 1) {strTsMin = "0" + strTsMin; }
if (strTsMon.length() == 1) { strTsMon = "0" + strTsMon; }
if (strTsDay.length() == 1) {strTsDay = "0" + strTsDay; }
if (strTsHour.length() == 1) {strTsHour = "0" + strTsHour; }
if (strTsSec.length() == 1) {strTsSec = "0" + strTsSec; }
strTimeStamp = strTsYear + "-" + strTsMon + "-" + strTsDay + " " + strTsHour + ":"
+ strTsMin + ":" + strTsSec;
iFree = rt.freeMemory();
iTotal = rt.totalMemory();
Free = new Float(iFree/10000);
Free = new Float(Free.floatValue() / 100.);
Total = new Float(iTotal/10000);
Total = new Float(Total.floatValue() / 100.);
Used = new Float((iTotal - iFree)/10000);
Used = new Float(Used.floatValue() / 100.);
ts = new Date();
out.println("Date :: " + strTimeStamp);
out.println("Server Name ::" + request.getServerName());
out.println("Port ::" + request.getServerPort());
out.println("Used Memory :: " + Used.floatValue()+ " (MB)");
out.println("Free Memory :: " + Free.floatValue()+ " (MB)");
out.println("Total Memory :: " + Total.floatValue() + " (MB)");
%>


Date :: 2011-11-28 05:50:59
Server Name :: brigitz.com
Port :: 443
Used Memory :: 19.74 (MB)
Free Memory :: 46.97 (MB)
Total Memory :: 66.71 (MB)
OUTPUT: