001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.samples.daytrader.web.prims; 018 019 import java.io.*; 020 import javax.servlet.*; 021 import javax.servlet.http.*; 022 023 import org.apache.geronimo.samples.daytrader.util.*; 024 025 026 /** 027 * 028 * ExplicitGC invokes System.gc(). This allows one to gather min / max heap statistics. 029 * 030 */ 031 public class ExplicitGC extends HttpServlet 032 { 033 034 private static String initTime; 035 private static int hitCount; 036 037 /** 038 * forwards post requests to the doGet method 039 * Creation date: (01/29/2006 20:10:00 PM) 040 * @param res javax.servlet.http.HttpServletRequest 041 * @param res2 javax.servlet.http.HttpServletResponse 042 */ 043 public void doPost(HttpServletRequest req, HttpServletResponse res) 044 throws ServletException, IOException 045 { 046 doGet(req, res); 047 } 048 049 /** 050 * this is the main method of the servlet that will service all get requests. 051 * @param request HttpServletRequest 052 * @param responce HttpServletResponce 053 **/ 054 public void doGet(HttpServletRequest req, HttpServletResponse res) 055 throws ServletException, IOException 056 { 057 try 058 { 059 res.setContentType("text/html"); 060 061 ServletOutputStream out = res.getOutputStream(); 062 hitCount++; 063 long totalMemory = Runtime.getRuntime().totalMemory(); 064 065 long maxMemoryBeforeGC = Runtime.getRuntime().maxMemory(); 066 long freeMemoryBeforeGC = Runtime.getRuntime().freeMemory(); 067 long startTime = System.currentTimeMillis(); 068 069 System.gc(); // Invoke the GC. 070 071 long endTime = System.currentTimeMillis(); 072 long maxMemoryAfterGC = Runtime.getRuntime().maxMemory(); 073 long freeMemoryAfterGC = Runtime.getRuntime().freeMemory(); 074 075 076 077 out.println( 078 "<html><head><title>ExplicitGC</title></head>" 079 + "<body><HR><BR><FONT size=\"+2\" color=\"#000066\">Explicit Garbage Collection<BR></FONT><FONT size=\"+1\" color=\"#000066\">Init time : " 080 + initTime 081 + "<BR><BR></FONT> <B>Hit Count: " 082 + hitCount 083 + "<br>" 084 + "<table border=\"0\"><tr>" 085 + "<td align=\"right\">Total Memory</td><td align=\"right\">" + totalMemory + "</td>" 086 + "</tr></table>" 087 + "<table width=\"350\"><tr><td colspan=\"2\" align=\"left\">" 088 + "Statistics before GC</td></tr>" 089 + "<tr><td align=\"right\">" 090 + "Max Memory</td><td align=\"right\">" + maxMemoryBeforeGC + "</td></tr>" 091 + "<tr><td align=\"right\">" 092 + "Free Memory</td><td align=\"right\">" + freeMemoryBeforeGC + "</td></tr>" 093 + "<tr><td align=\"right\">" 094 + "Used Memory</td><td align=\"right\">" + (totalMemory - freeMemoryBeforeGC) + "</td></tr>" 095 + "<tr><td colspan=\"2\" align=\"left\">Statistics after GC</td></tr>" 096 + "<tr><td align=\"right\">" 097 + "Max Memory</td><td align=\"right\">" + maxMemoryAfterGC + "</td></tr>" 098 + "<tr><td align=\"right\">" 099 + "Free Memory</td><td align=\"right\">" + freeMemoryAfterGC + "</td></tr>" 100 + "<tr><td align=\"right\">" 101 + "Used Memory</td><td align=\"right\">" + (totalMemory - freeMemoryAfterGC) + "</td></tr>" 102 + "<tr><td align=\"right\">" 103 + "Total Time in GC</td><td align=\"right\">" + Float.toString((endTime - startTime) / 1000) + "s</td></tr>" 104 + "</table>" 105 + "</body></html>"); 106 } 107 catch (Exception e) 108 { 109 Log.error(e, "ExplicitGC.doGet(...): general exception caught"); 110 res.sendError(500, e.toString()); 111 112 } 113 } 114 115 /** 116 * returns a string of information about the servlet 117 * @return info String: contains info about the servlet 118 **/ 119 public String getServletInfo() 120 { 121 return "Generate Explicit GC to VM"; 122 } 123 /** 124 * called when the class is loaded to initialize the servlet 125 * @param config ServletConfig: 126 **/ 127 public void init(ServletConfig config) throws ServletException 128 { 129 super.init(config); 130 initTime = new java.util.Date().toString(); 131 hitCount = 0; 132 133 } 134 }